使用facet_wrap时数据在x轴上移动

时间:2019-05-18 12:52:59

标签: r ggplot2 facet-wrap

我正在创建2014年至2018年每个月的辐照度分布。前三个月的图表看起来很不错,因为每年都存在点,然后数据点开始缓慢地向轴右侧移动,并且到12月时,2018数据将显示在2019栏中。

我是R语言中的一个相对较新的用户,对下面的内容并不了解。在这里,我附上我的代码和图表。月份名称是通过基本库中Month <-months(a)的简单命令创建的。请帮助我。

ggplot(data = Weather_new, aes(x=DateAndTime, y= KP_sensor), na.rm=TRUE) + 
 geom_point(color = "darkblue", alpha=0.2)+
  facet_wrap(.~Monthnames, ncol=4, strip.position = "top")+
  labs(title = "Irradiance distribution over the years")+ xlab('Years')+ 
  ylab(expression("Irradiance"~"["*W / m^2*"]"))+
  theme(plot.title = element_text(face = "bold"))+
  theme(plot.title = element_text(hjust = 0.5))

**Image of Irradiance distribution overt the 5 years from 2014 to 2018**

1 个答案:

答案 0 :(得分:0)

考虑添加年份列,并将日期时间字段替换为aes中的 x 值:

Weather_new <- transform(Weather_new, Year = factor(format(DateAndTime, "%Y")))

ggplot(data = Weather_new, aes(x=Year, y= KP_sensor), na.rm=TRUE) + 
  geom_point(color = "darkblue", alpha=0.2) +
  facet_wrap(.~Monthnames, ncol=4, strip.position = "top") +
  labs(title = "Irradiance distribution over the years") +
  xlab('Years') + ylab(expression("Irradiance"~"["*W / m^2*"]")) +
  theme(plot.title = element_text(face = "bold")) +
  theme(plot.title = element_text(hjust = 0.5))