R ggplot facet_使用不同的y轴标签、一个值和一个百分比进行包裹
我正在使用ggplot中的R ggplot facet_使用不同的y轴标签、一个值和一个百分比进行包裹,r,ggplot2,time-series,visualization,facet-wrap,R,Ggplot2,Time Series,Visualization,Facet Wrap,我正在使用ggplot中的facet\u wrap绘制时间序列值及其百分比: 对于下面的图,上面的图是值,下面的图是百分比变化。我希望下面图中的y轴是“%”。通常在ggplot中,我会这样做 + scale_y_continuous(labels = scales::percent) 但是由于我使用的是facet_wrap,如何指定我只希望两个绘图的y轴标签中的一个是百分比 注意:以下是生成此图的代码: library(data.table) library(ggplot2) library
facet\u wrap
绘制时间序列值及其百分比:
对于下面的图,上面的图是值,下面的图是百分比变化。我希望下面图中的y轴是“%”。通常在ggplot中,我会这样做
+ scale_y_continuous(labels = scales::percent)
但是由于我使用的是facet_wrap,如何指定我只希望两个绘图的y轴标签中的一个是百分比
注意:以下是生成此图的代码:
library(data.table)
library(ggplot2)
library(scales)
library(dplyr)
pct <- function(x) {x/lag(x)-1}
Dates = seq(from = as.Date("2000-01-01"),
to =as.Date("2018-10-01"),
by = "1 month")
set.seed(1024)
this_raw = data.frame(CM = Dates,
value = rnorm(n = length(Dates)),
variable = rep("FAKE",length(Dates)))
this_diff = na.omit(as.data.table(this_raw %>%
group_by(variable) %>%
mutate_each(funs(pct), c(value))))
this_diff$type = "PerCng"
this_raw$type = "RAW"
plot_all = rbindlist(list(this_raw,this_diff))
plot_all$type = factor(plot_all$type, levels = c("RAW", "PerCng"))
out_gg = plot_all %>%
ggplot(aes(x=CM, y=value)) +
geom_line(color = "royalblue3") +
theme(legend.position='bottom')+
ggtitle("FAKE DATA") +
facet_wrap(~ type, scale = "free_y", nrow = 2,
strip.position = "left",
labeller = as_labeller(c(RAW = "Original", PerCng = "% Change") ) )+
scale_x_date(date_breaks = "12 month", date_labels = "%Y-%m",
date_minor_breaks = "3 month")+
ylab("")+
theme(plot.title = element_text(hjust = 0.5,size = 12),
axis.text.x = element_text(size = 6,angle = 45, hjust = 1),
axis.text.y = element_text(size = 6),
axis.title.y = element_text(size = 6)) +
theme(strip.background = element_blank(),
strip.placement = "outside")+
theme(legend.title=element_blank())
print(out_gg)
库(data.table)
图书馆(GG2)
图书馆(比例尺)
图书馆(dplyr)
百分比
分组依据(变量)%>%
变异每个(funs(pct),c(value)))
此_diff$type=“PerCng”
此\u raw$type=“raw”
plot_all=rbindlist(列表(此原始,此差异))
plot_all$type=系数(plot_all$type,levels=c(“原始”、“PerCng”))
out\u gg=绘图\u所有%>%
ggplot(aes(x=CM,y=value))+
几何图形线(color=“royalblue3”)+
主题(图例.position='bottom')+
ggtitle(“假数据”)+
端面包裹(~type,scale=“free_y”,nrow=2,
strip.position=“left”,
贴标机=贴标机(c(原始=“原始”,PerCng=“%Change”))+
缩放x日期(日期间隔=“12个月”,日期标签=“%Y-%m”,
日期_次要_中断=“3个月”)+
ylab(“”)+
主题(plot.title=element\u text(hjust=0.5,size=12),
axis.text.x=元素\文本(大小=6,角度=45,hjust=1),
轴.text.y=元素_文本(大小=6),
axis.title.y=元素_文本(大小=6))+
主题(strip.background=element_blank(),
strip.placement=“外部”)+
主题(legend.title=element\u blank())
打印(输出)
我同意上面的评论,即facet实际上并不适用于此用例。将不同的地块对齐是正统的做法
这就是说,如果您已经有了一堆格式良好的ggplot对象,并且确实不想只为axis标签重构代码,那么您可以将它们转换为grob对象并在引擎盖下挖掘:
library(grid)
# Convert from ggplot object to grob object
gp <- ggplotGrob(out_gg)
# Optional: Plot out the grob version to verify that nothing has changed (yet)
grid.draw(gp)
# Also optional: Examine the underlying grob structure to figure out which grob name
# corresponds to the appropriate y-axis label. In this case, it's "axis-l-2-1": axis
# to the left of plot panels, 2nd row / 1st column of the facet matrix.
gp[["layout"]]
gtable::gtable_show_layout(gp)
# Some of gp's grobs only generate their contents at drawing time.
# Using grid.force replaces such grobs with their drawing time content (if you check
# your global environment, the size of gp should increase significantly after running
# the grid.force line).
# This step is necesary in order to use gPath() to generate the path to nested grobs
# (& the text grob for y-axis labels is nested rather deeply inside the rabbit hole).
gp <- grid.force(gp)
path.to.label <- gPath("axis-l-2", "axis", "axis", "GRID.text")
# Get original label
old.label <- getGrob(gTree = gp,
gPath = path.to.label,
grep = TRUE)[["label"]]
# Edit label values
new.label <- percent(as.numeric(old.label))
# Overwrite ggplot grob, replacing old label with new
gp = editGrob(grob = gp,
gPath = path.to.label,
label = new.label,
grep = TRUE)
# plot
grid.draw(gp)
库(网格)
#从ggplot对象转换为grob对象
gp我同意Gregor对此的评论。制作单独的绘图并将它们排列在一起会更容易,或者转换第二个方面的数据。我相信,就定制每个情节而言,刻面是相当固定的。github上有一个看起来很有前途的软件包: