我需要重現下圖

在 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 控制臺會顯示一個“ ”號,我不知道在那之后要添加什么。歡迎大家提出意見。謝謝!
uj5u.com熱心網友回復:
將您的資料轉換為長格式并將貨幣映射到形狀和顏色美學。
選擇顏色、大小和主題以使您的資料和系列之間的差異清晰可見也是一個好主意
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 = "")

uj5u.com熱心網友回復:
而不是手動指定顏色(在您的情況下,“青色”和“紫色”)將您的資料從“寬”轉換為“長”并創建一個新變數,將每個觀察分類為“美國”或“歐元”。這可以通過gather()from dplyr 來完成(也修正了您的拼寫錯誤):
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(
)

現在你有更少的代碼和更多的空間來進行一些額外的美學變化,如果你愿意的話。
uj5u.com熱心網友回復:
您可以使用該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 = "Year",
y = "Share of Total Pretax Income",
title = "Income Inequality",
colour = "",
shape = "")
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())

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/452992.html
