【问题标题】:Fixing problems with ggplot palette - how to create a gradient boxplot?修复 ggplot 调色板的问题 - 如何创建渐变箱线图?
【发布时间】:2017-08-23 03:26:11
【问题描述】:

各位,请帮忙。什么都试过了。经历了不同的选择。有因素和没有因素,有 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

看起来很完美。

但我无法为它创建调色板。想做橙色-蓝色,收入低,橙色,渐变成高/蓝色。

有时我有因子错误(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

更新!

我发现了一些东西。 link 但是当我尝试用 color = as.integer(Estimate), group = Estimate 来做这件事时,它变得很糟糕。所以,以某种方式是可能的。

更新 2!

这个打印屏幕展示了我想要的结果,但这是 Tableau

【问题讨论】:

  • 您是否在 ggplot 的 aes() 中包含了 fill = Estimate?否则scale_fill_XXX() 函数将不知道要使用什么映射。另外,我不认为将状态转换为因子在这里是相关的。
  • @Z.Lin,我做到了,但它创造了一个传奇,并没有解决问题。
  • 我认为我分享的帖子的主要观点是很难为箱线图填充渐变颜色,并且在可视化方面没有添加新信息。
  • 伙计们,放松一下,Hadleys 说不 :( Hadley Wickham‏ @hadleywickham 1m 1 分钟前

标签: r ggplot2 gradient boxplot palette


【解决方案1】:

我不熟悉 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"))

【讨论】:

    猜你喜欢
    • 2022-12-15
    • 2023-03-18
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2019-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多