[英]Need to arrange several plots (3*2) in a single page (grid.arrange) (With Unified X axis) and shifted y axis
我正在嘗試使用ggplot在單個頁面中刻面六個圖。 在3 * 2網格中。 並將第二列中所有圖的y標簽從左向右更改。 我試圖將所有6個地塊的總和只有一個比例。 圖表的第一和第二列如何具有單個x軸?
我有如下功能...
plot_4 <- function( for (i in 1:7) { var[i] = readline("enter the variable name \n")\n) {
# var [i] doesnt work, how to get multiple inputs in this case ?
#Next I am doing aggregation, and its fine
gg4 <- aggregate(cbind(get(var1),get(var2),get(var3),get(var4),get(var5),get(var6))~Ei+Mi+hours,a, FUN=mean)
#For future calling purpose I am using below vector to store the variable names
myvars<-c(var1,var2,var3,var4,var5,var6)
names(gg4)[4] <- var1
names(gg4)[5] <- var2
names(gg4)[6] <- var3
names(gg4)[7] <- var4
names(gg4)[8] <- var5
names(gg4)[9] <- var6
# Plotting for each variable
plot_exp <- function(i,plotvar) {
dat <- subset(gg4, var = myvars[i] ) # myvars [i] doesnt work, I dont know how to choose each #variable separately for each iterations here in R.
ggplot(gg4,aes_string(x="hours", y=eval(get(var)), fill = "Mi")) +
geom_point(aes(color= "Mi"), size = 3) +
geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") +
facet_grid(myvars~.)
}
#Arranging in grid
ll <- do.call(Map, c(plot_exp, expand.grid(i=1:6, plotvar=myvars, stringsAsFactors=F)))
do.call(grid.arrange, ll)
}
這是行不通的。 請幫助修復上述代碼中的錯誤,並按照我在開始時所述的方法進行一些更改。
我幾乎沒有做任何安排來使圖形變為我想要的圖形(除了右邊的Y軸,但是還可以),但是現在我無法編輯圖例標題。
ggplot(dd,aes(x=hours, y=value)) +
geom_point(aes(color=factor(Mi)), size = 4, alpha = I (0.5)) +
geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") +
facet_wrap(~variable, nrow=3, ncol=2,scales = "free_y")
圖例就像![在此處輸入圖像描述] [2]如何編輯圖例名稱,我不想將factor(Mi)作為其名稱。 請告訴我。 提前致謝。
我用過scale_fill_continuous(guide = guide_legend(title = "V"))
和scale_fill_continuous(name = "V")
但是兩者都不能在這里工作。
ggplot
多次循環並調用ggplot
,不如將數據融合到一個data.frame中,然后使用構面同時布置所有圖,然后繪制所有圖,這更好。 這將自動幫助協調x軸。
首先,讓我們從擁有myvars
地方開始。 例如
myvars <- c("Nphy", "Cphy", "CHLphy", "Nhet", "Chet", "Ndet")
現在,我們可以從gg4
提取這些值並“融化”它們,以便每個觀察值都位於其自己的行上。
dd <- melt(gg4,id.vars=c("Ei","Mi","hours"), measure.vars=myvars)
現在我們可以用
ggplot(dd,aes(x=hours, y=value, fill = Mi)) +
geom_point(aes(color=Mi), size = 3) +
geom_smooth(stat= "smooth" , alpha = I(0.01), method="loess", color = "blue") +
facet_wrap(~variable, nrow=3, ncol=2)
就是這樣!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.