【问题标题】:How to change symbol in legend without changing it in the plot如何更改图例中的符号而不在图中更改它
【发布时间】:2018-02-21 09:23:13
【问题描述】:

我正在尝试复制以下情节: Plot to replicate

我已经做到了:My plot

因此,唯一剩下要做的就是将图例中的符号更改为大圆形,而不是一条线穿过它们的小圆圈。如何在不使情节中的圆圈变大的情况下实现这一目标?

到目前为止,我使用以下代码创建了我的情节:

g <- ggplot(d, aes(x = Num.3.Syllable.Words / Num.Words,
                   y = Num.Words / Num.Sentences, colour = Educational.Level)) 

g +
  geom_point() + geom_smooth(method = "lm", se = FALSE) +
  facet_grid(Educational.Level ~ .) +
  scale_x_continuous(breaks = c(0.05, 0.15, 0.25), labels = scales::percent) +
  scale_y_continuous(breaks = c(10, 20)) +
  labs(x = "Share of words with 3+ syllables", 
       y = "Words per sentence",
       colour = "Educational level", 
       title = "Ad Copy Complexity in Magazines", 
       subtitle = "Arranged by Typical Readership")

我要添加什么?我是否使用指南功能?提前致谢。

【问题讨论】:

    标签: r ggplot2 legend


    【解决方案1】:

    您可以通过像这样覆盖指南来做到这一点...

    ggplot(mtcars, aes(hp, mpg, group=gear, color=as.factor(gear))) + 
      geom_point() + 
      geom_line() + 
      guides(color = guide_legend(override.aes = list(linetype = 0, size=5)))
    

    【讨论】:

      【解决方案2】:

      你没有提供一些可重现的例子,所以很难说,但我建议你在 scale_colour_manual() 中使用 override.aes() 和 guide_legend() 元素,因为你可以更好地控制图例元素。

      示例

      使用链接here 的其他数据集我制作了这个plot,这正是您要寻找的。我使用此代码来执行此操作:

      library(dplyr)
      
      df <- df %>% filter(settlement_name_english == "JERUSALEM" |
                            settlement_name_english ==  "TEL AVIV - YAFO" |
                            settlement_name_english == "HAIFA")
      
      g <- ggplot(df, aes(x = votes, y = Registered_voters, colour = settlement_name_english))
      
      g +
        geom_point() +
        geom_smooth(method = "lm", se = FALSE) +
        facet_grid(settlement_name_english ~ .) +
        scale_colour_manual(values = c("purple", "green", "blue"),
                            guide = guide_legend(override.aes = list(
                              shape = c(NA, NA, NA)))) + 
        scale_x_continuous(breaks = c(0, 200, 400, 600), labels = scales::percent) +
        scale_y_continuous(breaks = c(100, 200, 300, 400, 500, 600, 700, 800))
      

      使用代码

      你的代码应该是这样的:

      g <- ggplot(d, aes(x = Num.3.Syllable.Words / Num.Words, y = Num.Words / Num.Sentences, colour = Educational.Level))
      
      g +
        geom_point() +
        geom_smooth(method = "lm", se = FALSE) +
        facet_grid(Educational.Level ~ .) +
        scale_colour_manual(values = c("purple", "green", "blue"),
                            guide = guide_legend(override.aes = list(
                              shape = c(NA, NA, NA)))) + 
        scale_x_continuous(breaks = c(0, 200, 400, 600), labels = scales::percent) +
        scale_x_continuous(breaks = c(0.05, 0.15, 0.25), labels = scales::percent) +
        scale_y_continuous(breaks = c(10, 20)) +
        labs(x = "Share of words with 3+ syllables",
             y = "Words per sentence",
             colour = "Educational level",
             title = "Ad Copy Complexity in Magazines",
             subtitle = "Arranged by Typical Readership")
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-10-27
        • 1970-01-01
        • 1970-01-01
        • 2019-02-02
        • 2016-04-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多