How to change symbol in legend without changing it in the plot

∥☆過路亽.° 提交于 2020-01-15 10:28:11

问题


I'm trying to replicate the following plot: Plot to replicate

I have gotten as far as this: My plot

Hence, the only thing left to do is change the symbols in the legend to big round circles instead of small circles with a line going through them. How can I achieve this without making the circles in my plot bigger?

I used the following code to create my plot so far:

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

What do I add? Do I use the guides function? Thanks in advance.


回答1:


You can do this by overwriting the guides like this...

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:


You don't offered some reproducible example, so it's difficult to say, but I suggest you to use the override.aes() and guide_legend() elements in scale_colour_manual(), because gives you more control on legend elements.

Example:

Using other dataset linked here I made this plot which is what you are looking for. I use this code to do this:

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

Code to use:

Your code would be something like this:

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



来源:https://stackoverflow.com/questions/48902174/how-to-change-symbol-in-legend-without-changing-it-in-the-plot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!