首页 > 解决方案 > 尝试向 ggplot 和 ggsmooth 添加多个图例

问题描述

所以我试图用我在同一年的月收入来绘制美国人口月收入中位数的图。我还想根据学位水平显示我的月收入变化。到目前为止,这是我拥有的代码:

dput(Earnings_Year)

ggplot(data = Earnings_Year)+
  geom_smooth(mapping = aes(x=Year,y=Month_USD))+
  geom_point(mapping = aes(x=Year,y=Month_USD,color=Degree))+
  geom_smooth(mapping = aes(x=Year,y=Median_Household_Income_US))+
  geom_point(mapping = aes(x=Year,y=Median_Household_Income_US))+
  labs(title = "Earning Comparison to Population",
       subtitle = "Individual vs Population Median 2006-2021",
       caption = "*Statistica Research Department, Jan. 20, 2021.")+
  theme_cleveland()+
  theme(plot.title = element_text(color = "blue",
                                  size = 16,
                                  face = "bold"),
        plot.subtitle = element_text(size=10,
                                     face = "bold"),
        plot.caption = element_text(face = "italic"))

这是它产生的图片:

示例图像.png

我的问题是我获得的学位有一个图例,但我没有顶线的图例(或者换句话说,我在图例中没有任何内容指定顶线是家庭收入中位数) . 您可以建议的任何修复将不胜感激。谢谢!

这是 dput 吐出的内容:

结构(列表(年 = 2006:2021,Month_USD = c(1160L,1240L,1360L,1480L,1320L,1320L,375L,1600L,2000L,2000L,1600L,2240L,1900L,2300L,2900L) (“高中”,“高中”,“高中”,“高中”,“高中”,“高中”,“高中”,“高中”,“高中”,“BA”, “BA”,“BA”,“BA”,“BA”,“教育硕士”,“教育硕士”),国家 = c(“美国”,“美国”,“美国”,“美国”,“美国”、“美国”、“德国”、“美国”、“美国”、“美国”、“美国”、“美国”、“中国”、“中国”、“中国”、“香港”)、工作 = c("洗碗机", "准备", "准备","Prep", "Prep", "Prep", "Au Pair", "CSA", "Valet", "Valet", "Intake", "CM", "Teacher", "Teacher", "Teacher", "学生”,Median_household_income_us = C(4833L,4961L,4784L,4750L,4750L,4626L,4556L,4547L,4706L,4706L,4634L,4873L,4873L,5025L,5218L,5218L,5218L,5360L,NA3.273.22573.2573.2573.2.173.HAND,5725; , 146.69, 140.64, 135.16, 143.88, 159.3, 166.5, 175.17, 184.51, 195.99, 204.9, 212.59, 236.31, NA)), class = "data.frame", row.names = c(NA, -16L))Median_Household_Income_US = c(4833L, 4961L, 4784L, 4750L, 4626L, 4556L, 4547L, 4706L, 4634L, 4873L, 5025L, 5218L, 5360L, 5725L, NA, NA), US_Home_Price_Index = c(183.24, 173.36, 152.56, 146.69, 140.64 , 135.16, 143.88, 159.3, 166.5, 175.17, 184.51, 195.99, 204.9, 212.59, 236.31, NA)), class = "data.frame", row.names = c(NA, -16L))Median_Household_Income_US = c(4833L, 4961L, 4784L, 4750L, 4626L, 4556L, 4547L, 4706L, 4634L, 4873L, 5025L, 5218L, 5360L, 5725L, NA, NA), US_Home_Price_Index = c(183.24, 173.36, 152.56, 146.69, 140.64 , 135.16, 143.88, 159.3, 166.5, 175.17, 184.51, 195.99, 204.9, 212.59, 236.31, NA)), class = "data.frame", row.names = c(NA, -16L))

标签: rggplot2

解决方案


如果您包含数据样本(例如使用dput(head(Earnings_Year))),您的问题会更清楚,但也许这个可重现的示例将帮助您解决问题:

library(tidyverse)

dat1 <- tibble(year = 2001:2021,
               pop_mean = sample(4000:6000, size = 21),
               ind_earnings = sample(1500:3000, size = 21),
               degree = c(rep("None", 7), rep("BA", 7), rep("M.Ed", 7)))

ggplot(dat1, aes(x = year)) +
  geom_smooth(aes(y=ind_earnings))+
  geom_point(aes(y=ind_earnings, color=degree))+
  geom_smooth(aes(y=pop_mean))+
  geom_point(aes(y=pop_mean, fill = "Median\nhousehold\nincome"))+
  labs(title = "Earning Comparison to Population",
       subtitle = "Individual vs Population Median 2006-2021",
       caption = "*Statistica Research Department, Jan. 20, 2021.")+
  theme_minimal()+
  theme(plot.title = element_text(color = "blue",
                                  size = 16,
                                  face = "bold"),
        plot.subtitle = element_text(size=10,
                                     face = "bold"),
        plot.caption = element_text(face = "italic")) +
  scale_fill_discrete(name = "")
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'
#> `geom_smooth()` using method = 'loess' and formula 'y ~ x'

reprex 包于 2021-08-10 创建 (v2.0.0 )


推荐阅读