r中一个变量的折线图,如何着色?

r中一个变量的折线图,如何着色?,r,ggplot2,linechart,R,Ggplot2,Linechart,因此,我认为这将是简单的使用以下: df <- structure(list(time_bin = c("00", "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", &q

因此,我认为这将是简单的使用以下:

df <- structure(list(time_bin = c("00", "01", "02", "03", "04", "05", 
"06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", 
"17", "18", "19", "20", "21", "22", "23"), count = c(33L, 35L, 
35L, 27L, 24L, 22L, 47L, 73L, 84L, 90L, 131L, 122L, 91L, 97L, 
78L, 70L, 56L, 50L, 54L, 37L, 40L, 25L, 24L, 31L)), row.names = c(NA, 
-24L), class = c("tbl_df", "tbl", "data.frame"))
您可以尝试以下方法:

ggplot(df, aes(y=count, x=as.factor(time_bin),group=1 )) +
  geom_line(color = "mediumpurple2") +
  labs(x = "Day of the Week", y = "Injury", fill = "") +
  lims(y = c(0,150)) +
  theme(axis.text.x=element_text(angle = 45))
输出:


你好,鸭子,但是我的数据不能正常工作怎么办?像这样的数据需要group参数吗?@JohnThomas如果只有一个var,可以使用
group=1
,但如果希望有更多行,则应该在数据帧中使用其他变量进行分组。同样的变量也可以用于颜色,然后您可以使用
scale\u color\u manual()
添加所需的颜色。如果有帮助,请告诉我:)
ggplot(df, aes(y=count, x=as.factor(time_bin),group=1 )) +
  geom_line(color = "mediumpurple2") +
  labs(x = "Day of the Week", y = "Injury", fill = "") +
  lims(y = c(0,150)) +
  theme(axis.text.x=element_text(angle = 45))