我正在嘗試使用 St Louis Fed 資料,使用fredr
包和管道(%>%) command in
ggplot2 . I am struggling to use the
xlim() or
lims()` 命令)繪制一個圖。
我可以使用以下代碼獲得我想要繪制的系列。
#libraries
library(tidyverse)
library(fredr)
library(ggthemes)
#make the plot
map_dfr(c("LABSHPUSA156NRUG", "W273RE1A156NBEA"), fredr) %>%
pivot_wider(
names_from = series_id,
values_from = value) %>%
mutate(., labour_share_of_profit = LABSHPUSA156NRUG/W273RE1A156NBEA)
到目前為止,一切都很好。這為我提供了我需要的資料,并帶有一個名為labour_share_of_profit
. 資料(使用put()
在問題的底部:
> . # A tibble: 93 × 6
date realtime_start realtime_end LABSHPUSA156NRUG W273RE1A156…1 labou…2
<date> <date> <date> <dbl> <dbl> <dbl>
1 1950-01-01 2022-11-13 2022-11-13 0.628 5.7 0.110
2 1951-01-01 2022-11-13 2022-11-13 0.634 5 0.127
3 1952-01-01 2022-11-13 2022-11-13 0.645 5.1 0.126
4 1953-01-01 2022-11-13 2022-11-13 0.644 4.8 0.134
5 1954-01-01 2022-11-13 2022-11-13 0.637 5.2 0.123
6 1955-01-01 2022-11-13 2022-11-13 0.627 6.1 0.103
7 1956-01-01 2022-11-13 2022-11-13 0.640 5.6 0.114
8 1957-01-01 2022-11-13 2022-11-13 0.639 5.3 0.121
9 1958-01-01 2022-11-13 2022-11-13 0.636 4.8 0.132
10 1959-01-01 2022-11-13 2022-11-13 0.629 5.7 0.110
# … with 83 more rows, and abbreviated variable names 1?W273RE1A156NBEA,
# 2?labour_share_of_profit
# ? Use `print(n = ...)` to see more rows
>
因此,要將其傳遞給我使用的繪圖代碼:%>%
ggplot(data = ., mapping = aes(x = date, y =labour_share_of_profit))
geom_line(lwd=1.2)
labs(x = "Year", y = "Share of Labour Compensation as Proportion of Profit")
theme(legend.position = "none")
theme_wsj()
%>%
{ggsave(filename = "p1_wsj.pdf",
device = "pdf",
width = 10*sqrt(2), height = 10)
}
這將產生以下圖。
現在,如何使用該xlim()
函式設定系列開頭的繪圖限制?下面的兩條評論不起作用。我認為這可能是因為日期資訊的存盤方式,以及它需要如何傳遞給ggplot
.
put
用于再現性的資料是:
structure(list(date = structure(c(-7305, -6940, -6575, -6209,
-5844, -5479, -5114, -4748, -4383, -4018, -3653, -3287, -2922,
-2557, -2192, -1826, -1461, -1096, -731, -365, 0, 365, 730, 1096,
1461, 1826, 2191, 2557, 2922, 3287, 3652, 4018, 4383, 4748, 5113,
5479, 5844, 6209, 6574, 6940, 7305, 7670, 8035, 8401, 8766, 9131,
9496, 9862, 10227, 10592, 10957, 11323, 11688, 12053, 12418,
12784, 13149, 13514, 13879, 14245, 14610, 14975, 15340, 15706,
16071, 16436, 16801, 17167, 17532, 17897, -14975, -14610, -14245,
-13880, -13514, -13149, -12784, -12419, -12053, -11688, -11323,
-10958, -10592, -10227, -9862, -9497, -9131, -8766, -8401, -8036,
-7670, 18262, 18628), class = "Date"), realtime_start = structure(c(19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309), class = "Date"), realtime_end = structure(c(19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309, 19309,
19309, 19309), class = "Date"), LABSHPUSA156NRUG = c(0.628362894058228,
0.633731782436371, 0.644851565361023, 0.644496023654938, 0.637051999568939,
0.626844048500061, 0.640427887439728, 0.638684749603271, 0.635847270488739,
0.629354357719421, 0.636741697788239, 0.633703410625458, 0.629107296466827,
0.626384675502777, 0.624378800392151, 0.619359791278839, 0.622894942760468,
0.630079448223114, 0.634225606918335, 0.643875300884247, 0.64898556470871,
0.63764888048172, 0.639445066452026, 0.640666723251343, 0.640968561172485,
0.625560820102692, 0.62158989906311, 0.621596157550812, 0.622295022010803,
0.62261962890625, 0.624348878860474, 0.614216029644012, 0.616743326187134,
0.603852272033691, 0.601951777935028, 0.60230153799057, 0.607737004756927,
0.615972578525543, 0.62066638469696, 0.611858665943146, 0.615197896957397,
0.615098834037781, 0.620048463344574, 0.614196300506592, 0.607954382896423,
0.607377409934998, 0.607116162776947, 0.609609842300415, 0.623013257980347,
0.625988662242889, 0.637073159217834, 0.640336573123932, 0.629616677761078,
0.621445715427399, 0.617115139961243, 0.605635344982147, 0.605510890483856,
0.604003727436066, 0.604088604450226, 0.59113609790802, 0.587999582290649,
0.592656254768372, 0.595092236995697, 0.593056976795197, 0.594270586967468,
0.595646262168884, 0.593772530555725, 0.596151113510132, 0.594325959682465,
0.597091138362885, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), W273RE1A156NBEA = c(5.7,
5, 5.1, 4.8, 5.2, 6.1, 5.6, 5.3, 4.8, 5.7, 5.5, 5.4, 6.1, 6.4,
6.8, 7.4, 7.3, 6.8, 6.3, 5.4, 4.5, 5, 5.4, 5.1, 4, 4.7, 5.3,
5.7, 5.9, 5.2, 4, 4.7, 4.4, 5, 5.5, 5.5, 4.3, 4.3, 4.5, 4, 3.7,
4.3, 4.2, 4.4, 5.2, 5.5, 5.9, 6.3, 5.3, 5, 3.9, 3.9, 5.4, 6,
6.7, 6.7, 7, 5.6, 4.4, 5.8, 7.2, 7.2, 7.6, 7.4, 7.3, 6.9, 6.6,
6.8, 7.2, 7.3, 8.9, 7, 3.2, -0.9, -1.2, 2.6, 3.9, 5.6, 5.7, 4.2,
5.2, 6.7, 6, 5.4, 5.1, 5.2, 4.2, 3.7, 4.9, 6.5, 6.6, 7.2, 8.4
), labour_share_of_profit = c(0.110239104220742, 0.126746356487274,
0.126441483404122, 0.134270004928112, 0.122509999917104, 0.10276131942624,
0.114362122757094, 0.120506556528919, 0.132468181351821, 0.110413045213934,
0.11577121777968, 0.117352483449159, 0.103132343683086, 0.0978726055473089,
0.0918204118223751, 0.083697269091735, 0.085328074350749, 0.0926587423857521,
0.100670731256879, 0.119236166830416, 0.144219014379713, 0.127529776096344,
0.118415753046672, 0.125620926127714, 0.160242140293121, 0.13309804683036,
0.117281113030776, 0.109051957465055, 0.105473732544204, 0.119734544020433,
0.156087219715118, 0.130684261626386, 0.140168937769803, 0.120770454406738,
0.109445777806369, 0.10950937054374, 0.141334187152774, 0.143249436866405,
0.137925863265991, 0.152964666485786, 0.166269701880378, 0.143046240473903,
0.147630586510613, 0.139590068296953, 0.116914304403158, 0.110432256351818,
0.102901044538466, 0.0967634670318119, 0.117549671317047, 0.125197732448578,
0.163352092107137, 0.164188864903572, 0.116595681066866, 0.103574285904566,
0.0921067373076482, 0.0903933350719623, 0.086501555783408, 0.107857808470726,
0.137292864647779, 0.101920016880693, 0.0816666086514791, 0.0823133687178294,
0.0783016101310128, 0.0801428347020536, 0.081406929721571, 0.0863255452418673,
0.0899655349326856, 0.0876692813985488, 0.0825452721781201, 0.0817933066250527,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -93L), class = c("tbl_df",
"tbl", "data.frame"))
uj5u.com熱心網友回復:
只需使用coord_cartesian
. 您可以在此處設定 x 軸和 y 軸的范圍。例如xlim = c(1950,2020)
.
library(lubridate)
df %>%
mutate(date = year(date)) %>%
ggplot(mapping = aes(x = date, y =labour_share_of_profit))
geom_line(lwd=1.2)
labs(x = "Year", y = "Share of Labour Compensation as Proportion of Profit")
theme(legend.position = "none")
theme_wsj()
coord_cartesian(xlim = c(1950,2020))
uj5u.com熱心網友回復:
類似下面的內容將允許您設定自定義標簽范圍(盡管它不會限制/過濾資料):
ggplot(data = ., mapping = aes(x = date, y =labour_share_of_profit))
geom_line(lwd=1.2)
scale_x_continuous(breaks=1950:2020)
labs(x = "Year", y = "Share of Labour Compensation as Proportion of Profit")
theme(legend.position = "none")
theme_wsj()
uj5u.com熱心網友回復:
另一種選擇是洗掉資料中的值,這就是規模從 1929 開始NA
的原因:x
library(ggplot2)
library(ggthemes)
ggplot(subset(dat, !is.na(labour_share_of_profit)), aes(x = date, y = labour_share_of_profit))
geom_line(lwd = 1.2)
labs(x = "Year", y = "Share of Labour Compensation as Proportion of Profit")
theme(legend.position = "none")
theme_wsj()
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/532436.html
標籤:rggplot2管道
上一篇:R-ggplot2:geom_text字體大小縮放到視窗大小
下一篇:重命名基礎R中的因子水平