【问题标题】:Line graph for one variable in r, how to color?r中一个变量的折线图,如何着色?
【发布时间】:2020-08-10 21:44:17
【问题描述】:

所以我认为使用以下内容会很简单:

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(time_bin_counts, aes(y=count, x=as.factor(time_bin), color = "mediumpurple2"  )) +
  geom_line() +
 labs(x = "Day of the Week", y = "Injury", fill = "") +
  lims(y = c(0,150)) +
  theme_hc() +
  theme(axis.text.x=element_text(angle = 45))

【问题讨论】:

    标签: r ggplot2 linechart


    【解决方案1】:

    你可以试试这个:

    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 如果你只有一个变量,你可以使用group=1,但如果你想要更多的行,你应该有其他变量在你的数据框中进行分组。同样的变量也可以用于颜色,然后您可以使用scale_color_manual() 添加您想要的颜色。让我知道这是否有帮助:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-19
    • 2021-10-01
    • 2016-12-09
    • 2022-10-05
    • 2018-11-13
    • 2021-12-16
    • 2021-05-13
    相关资源
    最近更新 更多