与lapply循环循环以添加带有GGPLOT中一列中值的文本的错误
我正在跟进我的最后一个问题因为我有点贪婪,我想在我的地块上添加更多东西,所以我决定创建一个新帖子。
因此,总而言之,我想在一个循环中制作几个地块,有人建议使用Lapply,这在当时想要的一切都很好。但是现在,我想添加一个文本报告标题下的额外列的相应值。以下代码采用了@stefan的建议,我对其进行了一些更改。
因此,我尝试了一个示例的数据帧子集的代码,并且它运行良好,但是当与Lapply循环时,我获得了一个空GGPLOT对象的列表...
这是使用随机示例的代码:
# Random example data
set.seed(123)
n <- 20
Dta <- data.frame(
sample = rep(LETTERS[seq(n)], each = 100),
x = rep(letters[seq(n)], each = 100),
value = rnorm(100 * n)
)
library(ggplot2)
library(gghighlight)
library(patchwork)
## Test with one sample
temp_df = subset(Dta, Dta$sample == "A")
letter = paste("Expected letter :",unique(temp_df$x, sep = " "))
ggplot(Dta, aes(x=value, group=sample,
color = sample)) +
geom_density()+
labs(x = "value",
y = "Density")+
theme_classic()+
theme(legend.position="none",
plot.title = element_text(hjust = 0.5))+
gghighlight(sample == "A",
unhighlighted_params = list(colour = "grey90"))+
#Add title and text from column x
ggtitle("A")+
annotate("text", x = 0.5, y = 3, label = letter, fontface = 'italic')
rm(temp_df)
# All samples with the loop
plot_fun <- function(highlight) {
temp_df = subset(Dta, Dta$sample == highlight)
letter = paste("Expected letter :",unique(temp_df$x, sep = " "))
ggplot(Dta, aes(x=value, group= sample,
color = sample)) +
geom_density()+
labs(x = "value",
y = "Density")+
theme_classic()+
theme(legend.position="none",
plot.title = element_text(hjust = 0.5))+
gghighlight(sample == highlight,
unhighlighted_params = list(colour = "grey90"))+
#Add title and text from column x
ggtitle(highlight)+
annotate("text", x = 0.5, y = 3, label = letter, fontface = 'italic')
rm(temp_df)
rm(letter)
}
Samples <- unique(Dta$sample)
# Make plots
p <- lapply(Samples, plot_fun)
,然后当我删除+ antotate(“ text”,“ text”,x = = 0.5,y = 3,label =字母,fontface ='italic')
,它起作用。因此,添加注释会使整个Lapply步骤出错。
你的想法是什么?我在做什么错?是否有另一种方法可以做我想做的事?
感谢您的帮助!
I am following up my last question here because I got a bit greedy and I wanted to add something more to my plots so I decided I would create a new post.
So to summarize I wanted to make several plots in a loop and someone suggested using lapply which worked perfectly well for what I wanted at that time. But now, I would like to add a text reporting the corresponding value of an extra column under the title. The following code takes the suggestion of @stefan and I changed it a little bit.
So I tried my code for a subset of the dataframe for one sample and it works well but when looping with lapply I get a list of empty ggplot object...
Here is the code using a random example:
# Random example data
set.seed(123)
n <- 20
Dta <- data.frame(
sample = rep(LETTERS[seq(n)], each = 100),
x = rep(letters[seq(n)], each = 100),
value = rnorm(100 * n)
)
library(ggplot2)
library(gghighlight)
library(patchwork)
## Test with one sample
temp_df = subset(Dta, Dta$sample == "A")
letter = paste("Expected letter :",unique(temp_df$x, sep = " "))
ggplot(Dta, aes(x=value, group=sample,
color = sample)) +
geom_density()+
labs(x = "value",
y = "Density")+
theme_classic()+
theme(legend.position="none",
plot.title = element_text(hjust = 0.5))+
gghighlight(sample == "A",
unhighlighted_params = list(colour = "grey90"))+
#Add title and text from column x
ggtitle("A")+
annotate("text", x = 0.5, y = 3, label = letter, fontface = 'italic')
rm(temp_df)
# All samples with the loop
plot_fun <- function(highlight) {
temp_df = subset(Dta, Dta$sample == highlight)
letter = paste("Expected letter :",unique(temp_df$x, sep = " "))
ggplot(Dta, aes(x=value, group= sample,
color = sample)) +
geom_density()+
labs(x = "value",
y = "Density")+
theme_classic()+
theme(legend.position="none",
plot.title = element_text(hjust = 0.5))+
gghighlight(sample == highlight,
unhighlighted_params = list(colour = "grey90"))+
#Add title and text from column x
ggtitle(highlight)+
annotate("text", x = 0.5, y = 3, label = letter, fontface = 'italic')
rm(temp_df)
rm(letter)
}
Samples <- unique(Dta$sample)
# Make plots
p <- lapply(Samples, plot_fun)
This is the desired output for one sample.
And then when I remove the + annotate("text", x = 0.5, y = 3, label = letter, fontface = 'italic')
, it works. So adding the annotate makes the whole lapply step go wrong.
What would be your thoughts ? What am I doing wrong ? Is there maybe another way to do what I want ?
Thanks for your help people !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您的绘图函数返回
null
,这是rm(...)
代码行的结果。删除这些行,您的代码正常工作:The issue is that your plot function returns
NULL
which is the result of therm(...)
code lines at the end of your function. Remove these lines and your code works fine:那尝试这个呢?只需将图存储为P,然后在函数末尾返回变量P即可。它适用于函数
lapply()
。What about trying this? Just store the plot as p, then return to the variable p at the end of the function. It would work well for the function
lapply()
.