gpt4 book ai didi

r - 在循环中运行箱形图/水平线时,hline 索引没有改变的问题

转载 作者:行者123 更新时间:2023-12-01 10:34:41 25 4
gpt4 key购买 nike

找了好久都没找到解决办法。我正在用 ggplot2 中的一些水平线循环绘制箱线图。我想出了将线条美学设置“颜色”映射为图例中我想要的名称,以获得线条和箱线图的图例。但是,线条索引值似乎没有更新,并且绘制的是所有绘图上的相同线条值,特别是循环中最后一项的值。

我重现了鸢尾花数据集的问题如下:

library(dplyr)
library(ggplot2)
library(tidyr)
liris<-gather(iris,"Param","Value",1:4)
liris<-as.data.frame(liris)

indices<-unique(liris$Param)
plot_list<-list()
for (i in indices) {
sub<-filter(liris,Param==i)

min<-min(sub$Value)
max<-max(sub$Value)

g<-ggplot(sub,aes(x=Param,y=Value,fill=Param))
g<-g+geom_boxplot()
g<-g+facet_wrap(~Species)
g<-g+geom_hline(aes(yintercept=min,colour="Min Sepal Length"),linetype="dashed",show_guide=TRUE)
g<-g+geom_hline(aes(yintercept=max,colour="Max Sepal Length"),linetype="dashed",show_guide=TRUE)
g<-g+theme()+scale_color_manual(values=c("blue","black"))
g<-g+theme(legend.position="bottom")+guides(colour=guide_legend(title=NULL))
g<-g+theme(legend.position="bottom")+guides(fill=guide_legend(title=NULL))
g<-g+theme(legend.box="horizontal")

plot_list[[i]] = g

}

pdf("TEST.pdf",height = 8.5, width = 11,onefile=TRUE,paper="special")
for (i in indices) {
print(plot_list[[i]])
}
dev.off()

这会生成带有漂亮图例的图,但水平线的值都相同。我可以说这是用我对 ggplot2 的有限经验绘制线条美学的东西。如果我不映射它们并执行此操作,则行值会正确更改,但没有图例。

library(dplyr)
library(ggplot2)
library(tidyr)
liris<-gather(iris,"Param","Value",1:4)
liris<-as.data.frame(liris)

indices<-unique(liris$Param)
plot_list<-list()
for (i in indices) {
sub<-filter(liris,Param==i)

min<-min(sub$Value)
max<-max(sub$Value)

g<-ggplot(sub,aes(x=Param,y=Value,fill=Param))
g<-g+geom_boxplot()
g<-g+facet_wrap(~Species)
g<-g+geom_hline(yintercept=min,colour="black",linetype="dashed",show_guide=TRUE)
g<-g+geom_hline(yintercept=max,colour="blue",linetype="dashed",show_guide=TRUE)
g<-g+theme(legend.position="bottom")+guides(colour=guide_legend(title=NULL))
g<-g+theme(legend.position="bottom")+guides(fill=guide_legend(title=NULL))
g<-g+theme(legend.box="horizontal")

plot_list[[i]] = g

}

pdf("TEST2.pdf",height = 8.5, width = 11,onefile=TRUE,paper="special")
for (i in indices) {
print(plot_list[[i]])
}
dev.off()

任何反馈和想法都将非常受欢迎。非常感谢。

最佳答案

我对绘图的格式有点不以为然,但这里有一个解决方案,可以为您提供您正在寻找的不断变化的最小值和最大值。请记住,由于 geom_boxplot() 正在设置 xlims(),因此虚线始终构成绘图范围,但比例值会波动。

indices<-unique(liris$Param)
plot_list<-list()
for (i in indices) {
sub <- filter(liris,Param==i)
#min <- min(sub$Value)
#max <- max(sub$Value)

g <- ggplot(sub,aes(x=Param,y=Value)) +
geom_boxplot(fill = "green", alpha = .5) +
facet_wrap(~Species) +
geom_hline(aes(yintercept=min(Value),colour="Max"),linetype="dashed",show.legend=TRUE) +
geom_hline(aes(yintercept=max(Value),colour="Min"),linetype="dashed",show.legend=TRUE) +
scale_color_manual(values=c("blue","red")) +
theme_bw() +
theme(legend.position="bottom")+guides(colour=guide_legend(title=NULL)) +
theme(legend.position="bottom")+guides(fill=guide_legend(title=NULL)) +
theme(legend.box="horizontal") +
labs(title = paste0(i, " Facetted by Species"), x = "", y = "cm")

plot_list[[i]] <- g
}
pdf("TEST2.pdf",height = 8.5, width = 11,onefile=TRUE,paper="special")
for (i in plot_list) {
print(i)
}
dev.off()

实际上,您需要做的就是在各自的 geom_hline() 内部动态调用 min(Value) 和 max(Value) 函数,而不是在绘图构建之前。 aes() 中的任何元素都应该来自为绘图 (liris) 提供的数据,而不仅仅是您环境中的变量(最小值、最大值)

enter image description here

关于r - 在循环中运行箱形图/水平线时,hline 索引没有改变的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37848069/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com