我们可以在一个 ggplot 图例中显示两个属性吗?
Can we display two attributes in one ggplot legend?
阅读我发现的有关序数数据的图 this 图:
我发现这个图例是多余的,因为 10 的小频率,例如,总是一个小正方形 和 一个浅色正方形,等等。因此,我想创建一个带有这样图例的图(油漆解决方案):
重现情节的代码
# DATA
importance <- rep(1:5, times = c(30, 42, 75, 93, 60))
often <- c(rep(1:5, times = c(15, 07, 04, 03, 01)), #n=30, importance 1
rep(1:5, times = c(10, 14, 12, 03, 03)), #n=42, importance 2
rep(1:5, times = c(12, 23, 20, 13, 07)), #n=75, importance 3
rep(1:5, times = c(16, 14, 20, 30, 13)), #n=93, importance 4
rep(1:5, times = c(12, 06, 11, 17, 14))) #n=60, importance 5
running.df <- data.frame(importance, often)
runningcounts.df <- as.data.frame(table(importance, often))
# PLOT
theme_nogrid <- function (base_size = 12, base_family = "") {
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(panel.grid = element_blank())
}
ggplot(runningcounts.df, aes(importance, often)) +
geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
scale_size_continuous(range = c(3,15)) +
scale_color_gradient(low = "white", high = "black") +
theme_nogrid()
您可以尝试使用 guides
,在您的代码中添加 guides(color = guide_legend(), size = guide_legend())
行。
ggplot(runningcounts.df, aes(importance, often)) +
geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
scale_size_continuous(range = c(3,15)) +
scale_color_gradient(low = "white", high = "black") +
guides(color = guide_legend(), size = guide_legend()) +
theme_nogrid()
如果您想要快速修复,这应该可行
# PLOT
theme_nogrid <- function (base_size = 12, base_family = "") {
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(panel.grid = element_blank())
}
ggplot(runningcounts.df, aes(importance, often)) +
geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
scale_size_continuous(range = c(3,15)) +
scale_color_gradient(low = "white", high = "black") +
theme_nogrid()+
guides( colour = guide_legend()) #added guides()
阅读我发现的有关序数数据的图 this 图:
我发现这个图例是多余的,因为 10 的小频率,例如,总是一个小正方形 和 一个浅色正方形,等等。因此,我想创建一个带有这样图例的图(油漆解决方案):
重现情节的代码
# DATA
importance <- rep(1:5, times = c(30, 42, 75, 93, 60))
often <- c(rep(1:5, times = c(15, 07, 04, 03, 01)), #n=30, importance 1
rep(1:5, times = c(10, 14, 12, 03, 03)), #n=42, importance 2
rep(1:5, times = c(12, 23, 20, 13, 07)), #n=75, importance 3
rep(1:5, times = c(16, 14, 20, 30, 13)), #n=93, importance 4
rep(1:5, times = c(12, 06, 11, 17, 14))) #n=60, importance 5
running.df <- data.frame(importance, often)
runningcounts.df <- as.data.frame(table(importance, often))
# PLOT
theme_nogrid <- function (base_size = 12, base_family = "") {
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(panel.grid = element_blank())
}
ggplot(runningcounts.df, aes(importance, often)) +
geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
scale_size_continuous(range = c(3,15)) +
scale_color_gradient(low = "white", high = "black") +
theme_nogrid()
您可以尝试使用 guides
,在您的代码中添加 guides(color = guide_legend(), size = guide_legend())
行。
ggplot(runningcounts.df, aes(importance, often)) +
geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
scale_size_continuous(range = c(3,15)) +
scale_color_gradient(low = "white", high = "black") +
guides(color = guide_legend(), size = guide_legend()) +
theme_nogrid()
如果您想要快速修复,这应该可行
# PLOT
theme_nogrid <- function (base_size = 12, base_family = "") {
theme_bw(base_size = base_size, base_family = base_family) %+replace%
theme(panel.grid = element_blank())
}
ggplot(runningcounts.df, aes(importance, often)) +
geom_point(aes(size = Freq, color = Freq, stat = "identity", position = "identity"), shape = 15) +
scale_size_continuous(range = c(3,15)) +
scale_color_gradient(low = "white", high = "black") +
theme_nogrid()+
guides( colour = guide_legend()) #added guides()