Two lines of X axis labels in ggplot

时光毁灭记忆、已成空白 提交于 2020-03-19 05:36:11

问题


I want to make two lines of X axis labels in ggplot.

In this plot, I want to add one more line of label below each specified year. Something like

1990 1995 2000 2005 2010 cold warm warm cold warm

This is my code for making this plot

ggplot(subset(dat, countryid %in% c("1")),  aes(date, 
nonpartisan))+geom_line(aes(color=countryid), color="dodgerblue1", 
size=1.4)+geom_line(aes(date, reshuffle), color="gray")+ theme_bw()

Is there any way to make one more line of label by creating a column specifically for the labels?

Thanks!


回答1:


You can just add custom labels via scale_x_continuous (or scale_x_date, if it is actually in Date format).

ggplot(subset(dat, countryid %in% c("1")),  aes(date, nonpartisan)) +
  geom_line(aes(color=countryid), color="dodgerblue1", size=1.4) +
  geom_line(aes(date, reshuffle), color="gray") + 
  theme_bw() +
  scale_x_continuous(name = 'date', 
                     breaks = c('1990', '1995', '2000', '2005', '2010'), 
                     labels = c('1990\ncold', '1995\nwarm', '2000\nwarm', '2005\ncold', '2010\nwarm'))


来源:https://stackoverflow.com/questions/27418222/two-lines-of-x-axis-labels-in-ggplot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!