R:120列的ggplot()递归打印为png
我有一个融化的数据集:R:120列的ggplot()递归打印为png,r,ggplot2,R,Ggplot2,我有一个融化的数据集: ID variable mean sd sem 1 0001 1 0.000000000 0.000000000 0.000000000 2 0001 2 0.000000000 0.000000000 0.000000000 3 0001 3 1.374013050 0.083787761 0.001524927 4 0001 4 1.622939744 0.232510250 0
ID variable mean sd sem
1 0001 1 0.000000000 0.000000000 0.000000000
2 0001 2 0.000000000 0.000000000 0.000000000
3 0001 3 1.374013050 0.083787761 0.001524927
4 0001 4 1.622939744 0.232510250 0.004231658
5 0001 5 0.004092427 0.004076841 0.000074198
50个唯一ID中的每一个都有120个变量
[根据评论编辑]
我想为每个变量创建一个单独的绘图,显示每个ID的平均值和sem。我想对平均值进行排序
第一:
我想为一个变量创建一个绘图:
ggplot(subset(dataset, variable=="1"), aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
theme(axis.title.x = element_text(face="bold",size=16),
axis.text.x = element_text(angle=90, colour = "black", vjust=1, hjust = 1, size=14),
axis.text.y = element_text(colour = "black", size=14),
axis.title.y = element_text(face="bold", size=16),
plot.title = element_text(size = 18),
legend.title = element_text(size=14),
legend.text = element_text(size = 13),
legend.position="right",
strip.text.x = element_text(size=12, face="bold"),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(colour="black")) + ylab("Mean") + xlab("ID")
现在,我想对所有120个变量做同样的事情。如何初始化for语句以为每个变量创建绘图
我试过:
for(i in 1:120)
{unique <- unique(test$variable)
ggplot(unique[i], aes(x = KGID, y = mean))
for(1:120中的i)
{unique尝试在您的ggplot
调用周围添加一个print语句,因为ggplot只在循环中返回ggplot对象,不打印绘图。另外,我认为ggplot不会侦听par(mfrow(
。ggplot网格的标准是grid.arrange
,如中所示。您也可以重叠变量以将ggplot对象存储在列表中,然后通过do.call
命令中的arrange.grid
打印。希望这有帮助!尝试在ggplot
调用周围添加打印语句s as ggplot只返回循环中的ggplot对象,不打印绘图。另外,我不认为ggplot侦听par(mfrow(
。ggplot网格的标准是grid.arrange
,如中所示。您也可以重叠变量以将ggplot对象存储在列表中,然后通过do.call
命令中的arrange.grid
打印。希望这有帮助!尝试在ggplot
调用周围添加打印语句s as ggplot只返回循环中的ggplot对象,不打印绘图。另外,我不认为ggplot侦听par(mfrow(
。ggplot网格的标准是grid.arrange
,如中所示。您也可以重叠变量以将ggplot对象存储在列表中,然后通过do.call
命令中的arrange.grid
打印。希望这有帮助!尝试在ggplot
调用周围添加打印语句s as ggplot只返回循环中的ggplot对象,不打印绘图。另外,我不认为ggplot侦听par(mfrow(
。ggplots网格的标准是grid。如中所示排列
。您也可以重叠变量以将ggplot对象存储在列表中,并通过do使用arrange.grid
打印。按中所示调用
命令。希望这有帮助!我建议创建所有的绘图,然后保存它们
# par() works for base graphics, it does nothing for ggplot
# par(mfrow=c(3, 4)) #just try to plot 12 variables right now
创建绘图:
# initialize a list to put the plots in
my_plots = list()
u <- unique(dataset$variable) #Get all unique values for variable
for(i in 1:length(u)) {
# I just put your data subset inside the plot
my_plots[[i]] = ggplot(test[dataset$variable==u[i], ],
aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
# I deleted all your theme stuff to keep this minimal and
# easy to understand, you can add it back in
ylab("Mean") + xlab("ID")
}
将它们保存在for循环中:(也可以使用lappy
)
不过,如果我是你,我认为刻面可能会更好:
ggplot(dat, aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
ylab("Mean") + xlab("ID") +
facet_wrap(~ variable)
我建议创建所有绘图,然后保存它们
# par() works for base graphics, it does nothing for ggplot
# par(mfrow=c(3, 4)) #just try to plot 12 variables right now
创建绘图:
# initialize a list to put the plots in
my_plots = list()
u <- unique(dataset$variable) #Get all unique values for variable
for(i in 1:length(u)) {
# I just put your data subset inside the plot
my_plots[[i]] = ggplot(test[dataset$variable==u[i], ],
aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
# I deleted all your theme stuff to keep this minimal and
# easy to understand, you can add it back in
ylab("Mean") + xlab("ID")
}
将它们保存在for循环中:(也可以使用lappy
)
不过,如果我是你,我认为刻面可能会更好:
ggplot(dat, aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
ylab("Mean") + xlab("ID") +
facet_wrap(~ variable)
我建议创建所有绘图,然后保存它们
# par() works for base graphics, it does nothing for ggplot
# par(mfrow=c(3, 4)) #just try to plot 12 variables right now
创建绘图:
# initialize a list to put the plots in
my_plots = list()
u <- unique(dataset$variable) #Get all unique values for variable
for(i in 1:length(u)) {
# I just put your data subset inside the plot
my_plots[[i]] = ggplot(test[dataset$variable==u[i], ],
aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
# I deleted all your theme stuff to keep this minimal and
# easy to understand, you can add it back in
ylab("Mean") + xlab("ID")
}
将它们保存在for循环中:(也可以使用lappy
)
不过,如果我是你,我认为刻面可能会更好:
ggplot(dat, aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
ylab("Mean") + xlab("ID") +
facet_wrap(~ variable)
我建议创建所有绘图,然后保存它们
# par() works for base graphics, it does nothing for ggplot
# par(mfrow=c(3, 4)) #just try to plot 12 variables right now
创建绘图:
# initialize a list to put the plots in
my_plots = list()
u <- unique(dataset$variable) #Get all unique values for variable
for(i in 1:length(u)) {
# I just put your data subset inside the plot
my_plots[[i]] = ggplot(test[dataset$variable==u[i], ],
aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
# I deleted all your theme stuff to keep this minimal and
# easy to understand, you can add it back in
ylab("Mean") + xlab("ID")
}
将它们保存在for循环中:(也可以使用lappy
)
不过,如果我是你,我认为刻面可能会更好:
ggplot(dat, aes(x = ID, y = mean)) +
geom_bar(position = position_dodge(), stat = "identity") +
geom_errorbar(aes(ymin=mean-sem, ymax=mean+sem)) +
ylab("Mean") + xlab("ID") +
facet_wrap(~ variable)
惰性选项是(以mtcars为例)
或者,分两步
pl = plyr::dlply(mtcars, "carb", "%+%", e1 = p)
ggsave("plot%03d.png", gridExtra::marrangeGrob(pl, nrow=1, ncol=1))
惰性选项是(以mtcars为例)
或者,分两步
pl = plyr::dlply(mtcars, "carb", "%+%", e1 = p)
ggsave("plot%03d.png", gridExtra::marrangeGrob(pl, nrow=1, ncol=1))
惰性选项是(以mtcars为例)
或者,分两步
pl = plyr::dlply(mtcars, "carb", "%+%", e1 = p)
ggsave("plot%03d.png", gridExtra::marrangeGrob(pl, nrow=1, ncol=1))
惰性选项是(以mtcars为例)
或者,分两步
pl = plyr::dlply(mtcars, "carb", "%+%", e1 = p)
ggsave("plot%03d.png", gridExtra::marrangeGrob(pl, nrow=1, ncol=1))
我没有看到这个调用本身,是否缺少递归?我认为您对png
的调用为时过早。png
将只编写一个png
文件,这是一个图像,如果您没有调用png
,绘图窗口将显示任何内容。在一个png
中创建多个绘图将不起作用。获取打印以查看您想要的效果,然后只需调用png
,您就会没事了。我非常困惑您是想要一个带有多个条形图的单一打印,一个带有多个条形图面的单一打印,还是多个单独的打印。我也不确定您是想要多个png文件还是一个png文件作为输出。@Gregor。很抱歉,我没有我想的那么清楚ntended。我希望每个变量有一个图,每个图显示每个ID的平均值和sem。png
将保存一个图。(通常建议使用ggsave()
进行ggplots)。ggplots的一大好处是它们是对象。我建议您创建一个包含所有要创建的ggplots的列表,然后保存它们,而不是尝试将这两个步骤(创建和保存)混合在一起一起。我没有看到这个调用本身,我缺少递归吗?我认为你调用png
还为时过早。png
只会编写一个png
文件,这是一个图像,如果你没有调用png
,绘图窗口会显示什么。在一个png
中创建多个绘图将不起作用。让绘图按照您的要求进行查看,然后只需调用png
,您就可以了。我非常困惑您是想要一个带有多个条形图的绘图,一个带有多个条形图面的绘图,还是多个单独的绘图。我也不确定您是想要多个png文件还是一个png文件作为输出。@Gregor。很抱歉,我没有这样做ear如我所愿。我希望每个变量有一个图,每个图显示每个ID的平均值和sem。png
将保存一个图。(通常建议使用ggsave()
进行ggplots)。ggplots的一大好处是它们是对象。我建议您创建一个列表,列出您要创建的所有ggplots