如何在ggplot2中创建图例并更改点形状?

发布于 2025-01-18 01:40:36 字数 1404 浏览 4 评论 0 原文

重现下图

图我需要重现

我需要在 R Studio 上为宏观经济学项目 。我已经能够弄清楚其中的大部分内容,但给我带来最大问题的是图例和改变数据点的形状。这是到目前为止我所拥有的

手动输入的数据点

# `First I load up some packages`

library(tidyverse)

# `then I create vectors for the years and percentages for Europe and the US`

year <- c(1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 200, 2010)
us_percent <- c(40.5, 40.9, 44.7, 45.1, 36.5, 33.7, 34.1, 33.4, 37.5, 42.4, 46.9, 48.8)
 euro_percent <- c(45.9, 45.8, 39.5, 40.5, 33.8, 31.7, 31.6, 29.7, 29.4, 32.4, 34, 34.7)

# `then I create a data frame for my vectors and name it`

df <- data.frame(year, us_percent, euro_percent)

# `then I create a ggplot function for the US and Europe inequality`

ggplot() +
  geom_line(data=df, mapping=aes(x=year, y=euro_percent), color="cyan") +
  geom_point(data=df, mapping=aes(x=year, y=euro_percent), color="cyan", ) +
  geom_line(data=df, mapping=aes(x=year, y=us_percent), color="purple") +
  geom_point(data=df, mapping=aes(x=year, y=us_percent), color="purple") +
  xlim(1900, 2010) +
  ylim(25, 50) +
  labs(x="Year", y="Share of Total Pretax Income", title="Income Inequality")

这是我的代码,其中包含我尝试运行的 各种版本的 scale_color_manual 程序,但它会留下它,所以我的 R 控制台会显示一个“+”号,我不知道之后要添加什么。任何建议都将受到欢迎。谢谢你!

I need to reproduce the following figure

Figure I need to reproduce

on R Studio for a macroeconomics project. I've been able to figure most of it out, but the stuff that's giving me the biggest issue is the legend and changing the shapes of the data points. Here is what I have so far

How far I've gotten

Here is my code with the data points input manually

# `First I load up some packages`

library(tidyverse)

# `then I create vectors for the years and percentages for Europe and the US`

year <- c(1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 200, 2010)
us_percent <- c(40.5, 40.9, 44.7, 45.1, 36.5, 33.7, 34.1, 33.4, 37.5, 42.4, 46.9, 48.8)
 euro_percent <- c(45.9, 45.8, 39.5, 40.5, 33.8, 31.7, 31.6, 29.7, 29.4, 32.4, 34, 34.7)

# `then I create a data frame for my vectors and name it`

df <- data.frame(year, us_percent, euro_percent)

# `then I create a ggplot function for the US and Europe inequality`

ggplot() +
  geom_line(data=df, mapping=aes(x=year, y=euro_percent), color="cyan") +
  geom_point(data=df, mapping=aes(x=year, y=euro_percent), color="cyan", ) +
  geom_line(data=df, mapping=aes(x=year, y=us_percent), color="purple") +
  geom_point(data=df, mapping=aes(x=year, y=us_percent), color="purple") +
  xlim(1900, 2010) +
  ylim(25, 50) +
  labs(x="Year", y="Share of Total Pretax Income", title="Income Inequality")

I tried running various versions of scale_color_manual program, but it would leave it so my R console would show a "+" sign, and I don't know what to add after that. Any suggestions would be welcome. Thank you!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

够运 2025-01-25 01:40:36

将您的数据转换为长格式,并将货币映射到形状和颜色美学。

选择颜色、尺寸和主题也是一个好主意,使您的数据和系列之间的差异清晰可见

ggplot(tidyr::pivot_longer(df, -1, names_to = "Currency"), 
       aes(year, value, color = Currency)) +
  geom_line() +
  geom_point(aes(shape = Currency), size = 4) +
  scale_color_manual(values = c("orange2", "deepskyblue3")) +
  theme_light() +
  xlim(1900, 2010) +
  ylim(25, 50) +
  labs(x="Year", 
       y="Share of Total Pretax Income", 
       title="Income Inequality",
       color = "", shape = "")

< img src="https://i.sstatic.net/vH0zX.png" alt="在此处输入图像描述">

Pivot your data to long format and map the currency to shape and color aesthetics.

It's also a good idea to pick colors, sizes and themes that make your data and the differences between series clearly visible

ggplot(tidyr::pivot_longer(df, -1, names_to = "Currency"), 
       aes(year, value, color = Currency)) +
  geom_line() +
  geom_point(aes(shape = Currency), size = 4) +
  scale_color_manual(values = c("orange2", "deepskyblue3")) +
  theme_light() +
  xlim(1900, 2010) +
  ylim(25, 50) +
  labs(x="Year", 
       y="Share of Total Pretax Income", 
       title="Income Inequality",
       color = "", shape = "")

enter image description here

毁虫ゝ 2025-01-25 01:40:36

您可以使用 shape = 参数。通常,使用 ggplot2 ,建议在绘制之前以“长”格式使用数据集 - 如果您在 aes()作为形状和颜色。

library(tidyr)
library(ggplot2)

legend_labels <- c("Top 10% Income share: Europe", "Top 10% Income share: US")
df |> pivot_longer(cols = ends_with("percent"),
                   names_to = "area",
                   values_to = "percent") |> 
  ggplot(aes(x = year, y = percent, colour = area)) +
  geom_point(aes(shape = area)) +
  geom_line() +
  coord_cartesian(xlim = c(1900, 2010),
                  ylim = c(25, 50)) +
  labs(x = "",
       y = "Share of top income decile\nIn total pretax income\n(decennial averages)",
       title = "Income Inequality",
       colour = "",
       shape = "") +
  scale_x_continuous(breaks = seq(1900, 2020, 10)) +
  scale_colour_discrete(labels = legend_labels) +
  scale_shape_manual(values = c(2, 15), labels = legend_labels, ) +
  theme_bw() +
  theme(legend.position = "bottom",
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.y = element_blank())

You can use the shape = argument. In general with ggplot2 it's advisable to have a dataset in "long" format before plotting - much easier to then add a legend, if you add the stratification variables in the aes() as shapes and colours.

library(tidyr)
library(ggplot2)

legend_labels <- c("Top 10% Income share: Europe", "Top 10% Income share: US")
df |> pivot_longer(cols = ends_with("percent"),
                   names_to = "area",
                   values_to = "percent") |> 
  ggplot(aes(x = year, y = percent, colour = area)) +
  geom_point(aes(shape = area)) +
  geom_line() +
  coord_cartesian(xlim = c(1900, 2010),
                  ylim = c(25, 50)) +
  labs(x = "",
       y = "Share of top income decile\nIn total pretax income\n(decennial averages)",
       title = "Income Inequality",
       colour = "",
       shape = "") +
  scale_x_continuous(breaks = seq(1900, 2020, 10)) +
  scale_colour_discrete(labels = legend_labels) +
  scale_shape_manual(values = c(2, 15), labels = legend_labels, ) +
  theme_bw() +
  theme(legend.position = "bottom",
        panel.grid.minor.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.y = element_blank())

graph

虫児飞 2025-01-25 01:40:36

不要手动指定颜色(在您的情况下为“青色”和“紫色”),而是将数据从“宽”转换为“长”,并创建一个新变量,将每个观察结果分类为“美国”或“欧元”。这可以使用 dplyr 的 gather() 来完成(也修复了你的拼写错误):

Year <- c(1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010)
us_percent <- c(40.5, 40.9, 44.7, 45.1, 36.5, 33.7, 34.1, 33.4, 37.5, 42.4, 46.9, 48.8)
euro_percent <- c(45.9, 45.8, 39.5, 40.5, 33.8, 31.7, 31.6, 29.7, 29.4, 32.4, 34, 34.7)

df <- data.frame(Year, us_percent, euro_percent)

df_long <- df %>% gather(key = "Country", value = "Percentage", us_percent:euro_percent, factor_key = TRUE)
# Where: key is the new indicator name, value is the stacked data column 

然后,当你的数据设置并准备好时,你可以简单地定义功能美学(形状、颜色)在主 ggplot() 函数中,如下所示:

ggplot(
  data = df_long,
  aes(
    x = Year,
    y = Percentage, 
    color = Country, 
    shape = Country
  )
) + 
  geom_point() + 
  geom_line() +
  xlim(1900, 2010) +
  ylim(25, 50) +
  labs(
    x="Year", 
    y="Share of Total Pretax Income", 
    title="Income Inequality"
  ) + 
  theme_bw() + 
  scale_color_grey()

Income Inequality

现在你的代码更少了,并且有更多的空间来进行一些额外的美学改变,如果你想。

Instead of manually specifying the color (in your case, "cyan" and "purple") transform your data from 'wide' to 'long' and create a new variable that categorizes each observation into "US" or "EURO". This can be done with the gather() from dplyr (fixed your typos as well):

Year <- c(1900, 1910, 1920, 1930, 1940, 1950, 1960, 1970, 1980, 1990, 2000, 2010)
us_percent <- c(40.5, 40.9, 44.7, 45.1, 36.5, 33.7, 34.1, 33.4, 37.5, 42.4, 46.9, 48.8)
euro_percent <- c(45.9, 45.8, 39.5, 40.5, 33.8, 31.7, 31.6, 29.7, 29.4, 32.4, 34, 34.7)

df <- data.frame(Year, us_percent, euro_percent)

df_long <- df %>% gather(key = "Country", value = "Percentage", us_percent:euro_percent, factor_key = TRUE)
# Where: key is the new indicator name, value is the stacked data column 

Then, when you're data is set and ready, you can simply define the functional aesthetics (shape, color) within the main ggplot() function, like so:

ggplot(
  data = df_long,
  aes(
    x = Year,
    y = Percentage, 
    color = Country, 
    shape = Country
  )
) + 
  geom_point() + 
  geom_line() +
  xlim(1900, 2010) +
  ylim(25, 50) +
  labs(
    x="Year", 
    y="Share of Total Pretax Income", 
    title="Income Inequality"
  ) + 
  theme_bw() + 
  scale_color_grey()

Income Inequality

Now you've got less code and a bit more room to wiggle with some additional aesthetic changes, if you want.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文