Geom_point putting dots at wrong values

大城市里の小女人 提交于 2021-02-10 16:17:44

问题


I have the following data, with which I'm trying to make a pretty simple line plot with dots. For some reason, the first three points of 2015 get placed with a value above 1600 when their value is around 1300.

dput(data)
structure(list(y_value = c(1143L, 1133L, 1148L, 1155L, 1149L, 
1144L, 1181L, 1164L, 1178L, 1173L, 1176L, 1209L, 1284L, 1286L, 
1283L, 1288L, 1362L, 1396L, 1599L, 1583L, 1606L, 1645L, 1653L, 
1662L), Date = c("2014-1", "2014-2", "2014-3", "2014-4", "2014-5", 
"2014-6", "2014-7", "2014-8", "2014-9", "2014-10", "2014-11", 
"2014-12", "2015-1", "2015-2", "2015-3", "2015-4", "2015-5", 
"2015-6", "2015-7", "2015-8", "2015-9", "2015-10", "2015-11", 
"2015-12"), year = c(2014, 2014, 2014, 2014, 2014, 2014, 2014, 
2014, 2014, 2014, 2014, 2014, 2015, 2015, 2015, 2015, 2015, 2015, 
2015, 2015, 2015, 2015, 2015, 2015)), row.names = c(NA, -24L), class = c("tbl_df", 
"tbl", "data.frame"))

data %>%
  ggplot(aes(x = Date, y=y_value, group=year))+
  geom_line()+
  geom_point()+
  facet_grid(facets = .~year, scale="free", space="free", switch = "x")+
  theme_bw()+
  theme(axis.text.x = element_blank(),
        panel.spacing.x = unit(0,"line"))


Am I missing something obvious? Because I'm a bit baffled at this point.

Any help is much appreciated. Thanks!


回答1:


Because x is sorted alphabetically, convert to date, then use scale_x_date() to format the date labels:

data$Date <- as.Date(paste0(data$Date, "-1"), "%Y-%m-%d")

ggplot(data, aes(x = Date, y=y_value, group=year))+
  geom_line()+
  geom_point()+
  facet_grid(facets = .~year, scale="free", space="free", switch = "x")+
  scale_x_date() +
  theme_bw()+
  theme(axis.text.x = element_blank(),
        panel.spacing.x = unit(0,"line"))



来源:https://stackoverflow.com/questions/64811681/geom-point-putting-dots-at-wrong-values

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