Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 修复ggplot调色板的问题-如何创建渐变箱线图?_R_Ggplot2_Gradient_Boxplot_Palette - Fatal编程技术网

R 修复ggplot调色板的问题-如何创建渐变箱线图?

R 修复ggplot调色板的问题-如何创建渐变箱线图?,r,ggplot2,gradient,boxplot,palette,R,Ggplot2,Gradient,Boxplot,Palette,各位,请帮忙。什么都试过了。经历了不同的选择。有因子和无因子,有缩放填充手动和缩放填充梯度。。。不幸的是,什么都不管用:( 我创建了一个箱线图 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 =

各位,请帮忙。什么都试过了。经历了不同的选择。有因子和无因子,有缩放填充手动和缩放填充梯度。。。不幸的是,什么都不管用:(

我创建了一个箱线图

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
更新!

我发现了一些东西。 但是当我试图用color=as.integer(Estimate),group=Estimate来做这件事时,它变得不好了。因此,它是可能的。

更新2!

这个打印屏幕显示了我想要的结果,但这是一个表格

我对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"))

是否在ggplot的
aes()
中包含
fill=Estimate
函数不知道使用什么映射。此外,我认为将状态转换为因子在这里并不相关。可能相关:@Z.Lin,我做了,但它创建了一个图例,并没有解决问题。我认为我分享的帖子的要点是很难将渐变色填充到方框图中,并且它不会在视觉方面添加新信息伙计们,放松点,哈德利说不‏1分钟前已验证帐户@hadleywickham 1m不抱歉
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"))