目标:绘制如下图像:
特点: 1.两个不同的时间序列; 2.下面板有一个反向y轴; 3.阴影超过两个地块.
可能的解决方案:
1.刻面不合适 - (1)不能只使一个刻面的y轴反转,同时保持其他刻面不变.(2)难以逐个调整各个方面.
2.使用视口使用以下代码排列单个图:
library(ggplot2)
library(grid)
library(gridExtra)
##Import data
df<- read.csv("D:\\R\\SF_Question.csv")
##Draw individual plots
#the lower panel
p1<- ggplot(df, aes(TIME1, VARIABLE1)) + geom_line() + scale_y_reverse() + labs(x="AGE") + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000))
#the upper panel
p2<- ggplot(df, aes(TIME2, V2)) + geom_line() + labs(x=NULL) + scale_x_continuous(breaks = seq(1000,2000,200), limits = c(1000,2000)) + theme(axis.text.x=element_blank())
##For the shadows
#shadow position
rects<- data.frame(x1=c(1100,1800),x2=c(1300,1850),y1=c(0,0),y2=c(100,100))
#make shadows clean (hide axis, ticks, labels, background and grids)
xquiet …
Run Code Online (Sandbox Code Playgroud) 在我的真实研究世界中,在顶部(或顶部和底部)和右侧的y轴显示x轴是很常见的.但是,默认位置是底部的x和ggplot2中左边的y.
继Kohske张贴在这里,使用的命令是:
x <- seq(0, 10, 0.1)
y <- sin(x * pi)
qplot(x, y, geom = "line") +
scale_x_continuous(guide = guide_axis(position = "top")) +
scale_y_continuous(guide = guide_axis(position = "right"))
Run Code Online (Sandbox Code Playgroud)
我在开发模式下尝试了以上命令:
install_packages("devtools")
library(devtools)
dev_mode()
install_github("ggplot2", "kohske", "feature/pguide")
library(ggplot2)
Run Code Online (Sandbox Code Playgroud)
不幸的是,它与最新的plyr
软件包不兼容.消息:
The following 'from' values not present in 'x': col, color, pch, cex, lty, lwd, srt, adj, bg, fg, min, max...
Error in plyr:::split_indices(seq_len(nrow(data)), scale_id, n)
Run Code Online (Sandbox Code Playgroud)
Error in continuous_scale(c("x", "xmin", "xmax", "xend", "xintercept"), : …
Run Code Online (Sandbox Code Playgroud)