解决 ggplot 调色板的问题 - 如何创建渐变箱线图?
Fixing problems with ggplot palette - how to create a gradient boxplot?
亲们,请帮忙。尝试了一切。经历了不同的选择。有因素和没有因素,有 scale_fill_manual 和 scale_fill_gradient... 不幸的是,没有任何效果:(
我创建了一个箱线图
income.boxplot <- ggplot(income.by.state, aes(x = State, y = Estimate)) +
geom_boxplot() +
coord_flip() + scale_x_discrete(limits = rev(levels(income$State))) +
labs(title = "Median household income by state",
subtitle = "Source: 2011-2015 American Community Survey") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x = "State",
y = "Median household income",
fill = "Median household income") +
theme_fivethirtyeight() +
theme(axis.line.x = element_line(size = .5, colour = "black"),
axis.title = element_text(size = 14),
legend.position = "right",
legend.direction = "vertical",
legend.box = "vertical",
legend.key.size = unit(0.7, "cm"),
legend.text = element_text(size = 10),
text = element_text(family = "OfficinaSanITC-Book"),
plot.title = element_text(family = "OfficinaSanITC-Book"))
income.boxplot
看起来很完美。
但我无法为其创建调色板。想把低收入的橙蓝色做成橙色,然后渐变成 high/blue 颜色。
有时我有 因子错误(x = 状态,y = 估计):未使用的参数(y = 估计) 等等
我的数据看起来像
1 Ziebach County Median Household Income ($) South Dakota 35119
2 Zavala County Median Household Income ($) Texas 26672
3 Zapata County Median Household Income ($) Texas 32162
4 Yuma County Median Household Income ($) Colorado 43105
更新!
我发现了一些东西。
但是当我尝试用 color = as.integer(Estimate), group = Estimate 来做这个时,它变得糟糕了。所以,这是有可能的。
更新2!
这个打印屏幕展示了我想要的结果,但这是 Tableau
我对 Tableau 不熟悉,但看起来这本身并不是渐变,而是有些点根据它们所在的箱线图的四分位数进行着色。可以做到!
library(dplyr)
library(ggplot2)
data_frame(st = rep(state.name[1:5], each = 20),
inc = abs(rnorm(5*20))) %>%
group_by(st) %>%
mutate(bp = cut(inc, c(-Inf, boxplot.stats(inc)$stats, Inf), label = F)) %>%
ggplot(aes(st, inc)) +
stat_boxplot(outlier.shape = NA, width = .5) +
geom_point(aes(fill = factor(bp)), shape = 21, size = 4, alpha = 1) +
scale_fill_brewer(type = "div",
labels = c("1" = "Lowest outliers", "2" = "1st quartile",
"3" = "2nd quartile", "4" = "3rd quartile",
"5" = "4th quartile", "6" = "Highest outliers"))
亲们,请帮忙。尝试了一切。经历了不同的选择。有因素和没有因素,有 scale_fill_manual 和 scale_fill_gradient... 不幸的是,没有任何效果:(
我创建了一个箱线图
income.boxplot <- ggplot(income.by.state, aes(x = State, y = Estimate)) +
geom_boxplot() +
coord_flip() + scale_x_discrete(limits = rev(levels(income$State))) +
labs(title = "Median household income by state",
subtitle = "Source: 2011-2015 American Community Survey") +
theme(plot.title = element_text(hjust = 0.5)) +
labs(x = "State",
y = "Median household income",
fill = "Median household income") +
theme_fivethirtyeight() +
theme(axis.line.x = element_line(size = .5, colour = "black"),
axis.title = element_text(size = 14),
legend.position = "right",
legend.direction = "vertical",
legend.box = "vertical",
legend.key.size = unit(0.7, "cm"),
legend.text = element_text(size = 10),
text = element_text(family = "OfficinaSanITC-Book"),
plot.title = element_text(family = "OfficinaSanITC-Book"))
income.boxplot
看起来很完美。
但我无法为其创建调色板。想把低收入的橙蓝色做成橙色,然后渐变成 high/blue 颜色。
有时我有 因子错误(x = 状态,y = 估计):未使用的参数(y = 估计) 等等
我的数据看起来像
1 Ziebach County Median Household Income ($) South Dakota 35119
2 Zavala County Median Household Income ($) Texas 26672
3 Zapata County Median Household Income ($) Texas 32162
4 Yuma County Median Household Income ($) Colorado 43105
更新!
我发现了一些东西。
更新2!
这个打印屏幕展示了我想要的结果,但这是 Tableau
我对 Tableau 不熟悉,但看起来这本身并不是渐变,而是有些点根据它们所在的箱线图的四分位数进行着色。可以做到!
library(dplyr)
library(ggplot2)
data_frame(st = rep(state.name[1:5], each = 20),
inc = abs(rnorm(5*20))) %>%
group_by(st) %>%
mutate(bp = cut(inc, c(-Inf, boxplot.stats(inc)$stats, Inf), label = F)) %>%
ggplot(aes(st, inc)) +
stat_boxplot(outlier.shape = NA, width = .5) +
geom_point(aes(fill = factor(bp)), shape = 21, size = 4, alpha = 1) +
scale_fill_brewer(type = "div",
labels = c("1" = "Lowest outliers", "2" = "1st quartile",
"3" = "2nd quartile", "4" = "3rd quartile",
"5" = "4th quartile", "6" = "Highest outliers"))