【问题标题】:Customize lines in ggplot linegraph with multiple lines用多条线自定义ggplot折线图中的线
【发布时间】:2021-04-11 13:57:11
【问题描述】:

我构建了这个图表:

labels.minor <- c("nie","selten","manchmal", "mehrmals", "oft", "sehr oft", "immerzu")

df_ebf <- df_ebf %>%
  map_df(rev)


ggplot(data=df_ebf, aes(x=forcats::fct_inorder(Skalen), y=Werte, group="")) +
  geom_line(aes(y = Werte, color = "#003560")) +
  geom_line(aes(y = SD_plus, color = "#8DAE10", linetype = "dashed")) +
  geom_line(aes(y = SD_minus, color = "#8DAE10",linetype = "dashed")) +
  geom_point(color = "#003560") +
  coord_flip() +
  labs(x="EBF-Skalen") +
  scale_y_continuous(limits = c(0, 6), breaks = c(0,1,2,3,4,5,6), labels = paste0(0:6, "\n", labels.minor), sec.axis = sec_axis(~.x, breaks = 0:6)) +
  scale_x_discrete(expand = c(0,0)) +
  theme(panel.grid.major.y = element_blank(),panel.grid.minor.x = element_blank(),axis.line.x = element_line(size = 1, colour = "black", linetype=1),axis.title=element_blank())
 

但是没有改变线条的样式,样式只是出现在图例中。

【问题讨论】:

    标签: r ggplot2 linegraph


    【解决方案1】:

    将它们从 aes 中取出:

    aes(...), color="..", linetype=".."

    【讨论】:

    • 此时,您最好使用 pivot_longer 重塑数据,这将让您根据它们所在的集合对线条进行分组,并使用美学定义您应该拥有的形状和颜色,以及让它们在图例中正确表示。但这可能超出了这个级别的这个问题的范围
    猜你喜欢
    • 2022-01-14
    • 2014-09-27
    • 1970-01-01
    • 1970-01-01
    • 2019-05-24
    • 2019-10-27
    • 2012-11-23
    • 1970-01-01
    • 2016-04-06
    相关资源
    最近更新 更多