我的数据:
structure(list(YEAR = structure(1:10, .Label = c("1980", "1981",
"1982", "1983", "1984", "1985", "1986", "1987", "1988", "1989"
), class = "factor"), lupas = c(1185, 822, 1340, 853, 3018, 1625,
966, 1505, 1085, 1754)), .Names = c("YEAR", "lupas"), row.names = c(NA,
-10L), class = c("tbl_df", "tbl", "data.frame"), drop = TRUE)
我使用dplyr中的group_by对数据进行分组。
str(df1)
Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 10 obs. of 2 variables:
$ YEAR : Factor w/ 10 levels "1980","1981",..: 1 2 3 4 5 6 7 8 9 10
$ lupas: num 1185 822 1340 853 3018 ...
- attr(*, "drop")= logi TRUE
我正在尝试绘制年份的图表,以及每年的“lupas”。就像时间序列图一样。
这是我的代码,但是会出错:
ggplot(df1,
aes(x = YEAR, y = lupas)) +
geom_line()
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
这张图:
阅读此主题后:ggplot2 each group consists of only one observation
我已将代码更改为:
ggplot(df1,
aes(x = YEAR, y = lupas)) +
geom_line(position=position_dodge(.1))
现在我收到这条消息:
ymax not defined: adjusting position using y instead
geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
答案 0 :(得分:2)
也许这会有所帮助:
library(ggplot2)
p <- ggplot(df1, aes(x = YEAR, y = lupas, group = 1)) +
geom_point(color = "black") + geom_line(color = "blue")
修改强>
要在x轴上仅显示特定刻度,可以将scale_x_discrete()
与breaks
结合使用:
p <- ggplot(df1, aes(x = YEAR, y = lupas, group = 1)) +
geom_point(color = "black") + geom_line(color = "blue") +
scale_x_discrete(breaks = c("1980", "1985", "1989"))