如何更改图例中的符号而不在图中更改它

时间:2018-02-21 09:23:13

标签: r ggplot2 legend

我试图复制以下情节: 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")

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

2 个答案:

答案 0 :(得分:0)

您可以通过覆盖这样的指南来实现这一目标......

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)))

答案 1 :(得分:0)

你没有提供一些可重复的例子,所以很难说,但我建议你使用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")