I am trying to make a plot using St Louis Fed data, using the fredr
package and the pipe (%>%) command in
ggplot2. I am struggling to use the
xlim()or
lims()` command.
I can easily produce the plot I want (but without the right plot limits) with the following code.
#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) %>%
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)
}
This produces the following plot.
Now, how do I use the xlim()
function to set the plot limits for the beginning of the series?
CodePudding user response:
Something like the below will allow you to set custom label ranges (although it doesn't limit/filter the data):
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()
CodePudding user response:
Just use coord_cartesian
. Here you can set the range of the x- and y-axis. For example xlim = c(1950,2020)
.
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()
coord_cartesian(xlim = c(1950,2020))