gpt4 book ai didi

r - 指定图例列中的因子数

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

是否可以手动指示 ggplot 图例中每列显示的因子数?这是一个可重现的示例,展示了我正在尝试做的事情:

#loading libraries
library(ggplot2)

#Creating hypothetical dataframe
services<-data.frame(study=c(0),category=c(0),subcategory=c(0))
services<-services[-1,]
services[1:15,1]<-c(1:15)

services[1:4,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural")
services[5:8,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural")
services[9:10,2]<-c("Provisioning", "Regulating")
services[11:15,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural", "Provisioning")

services[1:4,3]<-c("Water supply", "Climate regulation", "Soil formation", "Recreation")
services[5:8,3]<-c("Fisheries", "Water purification", "Habitat", "Recreation")
services[9:10,3]<-c("Water supply", "Flood regulation")
services[11:15,3]<-c("Agriculture", "Water purification", "Soil formation", "Aesthetics", "Fisheries")

services

#Manually re-ordering subcategory factors by larger categories and by number of occurences in the df (I am also looking for a better way to do this, as it must exist!!)
table(services$subcategory)
services$subcategory <- factor(services$subcategory,
levels=c(#Cultural services
"Recreation", "Aesthetics",
#Provisioning services
"Fisheries", "Water supply", "Agriculture",
#Regulating services
"Water purification", "Climate regulation","Flood regulation",
#Supporting services
"Soil formation", "Habitat"))


#Creating figure
ggplot(services, aes(category, fill=subcategory)) + geom_bar() +
theme(legend.position="right") +
guides(fill=guide_legend(ncol=4, title="Ecosystem Service Sub Categories"))+
xlab("Ecosystem Service Type") +
ylab("Number of times each ecosystem service was evaluated")

The Figure produced by this code

What would like the figure legend to look like instead!

An Even more advanced way I'd love the figure to come out as

虽然this建议,和this一个明白这个想法,他们不会产生我正在寻找的结果。我已经思考这个问题好几个星期了,并且花了几个小时四处寻找解决方案,因为我反对必须在 power point、illustrator 等中手动进行这些调整。无法找到好的答案,我现在转向你伙计们!感谢您的帮助!

最佳答案

哈!虽然 Hadley 确认 ggplot2 本身不可能(请参阅评论中的链接),但有一种非常 hack-y 的方法可以做到这一点,它受到 ggplot2 中相关图例的最大美学数量的限制。

我所做的是创建四个虚拟数据框并在打印窗口外绘制四种不同的美学特征(大小、颜色、alpha、线型)。然后您可以覆盖图例美学,使它们看起来像填充颜色。关闭原始填充图例,瞧!

我知道组的顺序和组内的顺序并不是您想要的,但应该很容易随因子水平等进行更改。

原图

http://i.stack.imgur.com/LXmeo.png

新情节

http://i.imgur.com/1HyNdlV.png

代码

#####################
# Original code
#####################

#loading libraries
library(ggplot2)

#Creating hypothetical dataframe
services<-data.frame(study=c(0),category=c(0),subcategory=c(0))
services<-services[-1,]
services[1:15,1]<-c(1:15)

services[1:4,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural")
services[5:8,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural")
services[9:10,2]<-c("Provisioning", "Regulating")
services[11:15,2]<-c("Provisioning", "Regulating", "Suporting", "Cultural", "Provisioning")

services[1:4,3]<-c("Water supply", "Climate regulation", "Soil formation", "Recreation")
services[5:8,3]<-c("Fisheries", "Water purification", "Habitat", "Recreation")
services[9:10,3]<-c("Water supply", "Flood regulation")
services[11:15,3]<-c("Agriculture", "Water purification", "Soil formation", "Aesthetics", "Fisheries")
services$subcategory <- factor(services$subcategory, levels=c("Recreation", "Aesthetics","Fisheries", "Water supply", "Agriculture","Water purification", "Climate regulation","Flood regulation","Soil formation", "Habitat"))

#####################
# New code
#####################

# Create dummy variables
cultural <- data.frame(Cultural_Services = c("Recreation","Aesthetics"),category = c("Cultural","Cultural"))
provisional <- data.frame(Provisioning_Services = c("Fisheries","Water Supply","Agricultre"), category = c("Provisioning","Provisioning","Provisioning"))
regulating <- data.frame(Regulating_Services = c("Water Purification","Climate Regulation","Flood Regulation"), category = c("Regulating","Regulating","Regulating"))
supporting <- data.frame(Supporting_Services = c("Soil Formation","Soil Formation", "Habitat", "Habitat"), x = c("Regulating","Suporting","Regulating","Suporting"),y=c(-1,-1,-2,-2))

## Create plot
ggplot() +
## Plot the four dummy layer outside of the intended plotting area
geom_point(data = provisional, aes(x = category, y = -1, size = Provisioning_Services)) +
geom_point(data = cultural, aes(x = category, y = -1, color = Cultural_Services)) +
geom_point(data = regulating, aes(x = category, y = -1, alpha = Regulating_Services)) +
geom_line(data = supporting, aes(x = x, y = y, linetype = Supporting_Services)) +
## Add in your real plot goal
geom_bar(data = services, aes(category, fill=subcategory)) +
## Remove the Fill legend
scale_fill_hue(guide="none") +
## Override the guide aesthetics to make them look like fill colors
guides(size = guide_legend(override.aes = list(colour = c("#A3A500","#39B600","#00BF7D"),fill = c(NA,NA,NA), shape = c(15,15,15), size = c(8,8,8)),title="Provisioning Services"),
color = guide_legend(override.aes = list(colour = c("#F8766D","#D89000"), shape = c(15,15), size = c(8,8)),title = "Cultural Services"),
alpha = guide_legend(override.aes = list(colour = c("#00BFC4","#00B0F6","#9590FF"), shape = c(15,15,15),size = c(8,8,8)), title = "Regulating Services"),
linetype = guide_legend(override.aes = list(colour = c("#E76BF3","#FF62BC"), shape = c(15,15), size = c(8,8)),title = "Supporting Services")) +
## Adjust the plot range to hide all the extra layers
ylim(0,5) +
xlab("Ecosystem Service Type") +
ylab("Number of times each ecosystem service was evaluated")
#> Warning: Removed 3 rows containing missing values (geom_point).
#> Warning: Removed 2 rows containing missing values (geom_point).
#> Warning: Removed 3 rows containing missing values (geom_point).
#> geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

关于r - 指定图例列中的因子数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33376750/

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