1

Folks, please help. Tried everything. Went through different options. With factor and without, with scale_fill_manual and scale_fill_gradient... Nothing works, unfortunately :(

I created a boxplot

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

Looks perfectly. enter image description here

But I cannot create a palette for it. Wanted to do orange-blue with low income as orange and goes as gradient into high/blue color.

Sometime I have Error in factor(x = State, y = Estimate) : unused argument (y = Estimate) Etc.

My data looks like

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

Update!

I found something. link But when I tried to do this with color = as.integer(Estimate), group = Estimate, it became bad. So, it is possible somehow. enter image description here

Update 2!

This printscreen demonstrates my desired outcome, but this is Tableau enter image description here

Anakin Skywalker
  • 2,400
  • 5
  • 35
  • 63
  • 2
    Did you include `fill = Estimate` inside the `aes()` for ggplot? Else the `scale_fill_XXX()` function won't know what mapping to use. Also, I don't think converting state to factor is relevant here. – Z.Lin Aug 23 '17 at 04:11
  • 1
    Could be relevant: https://stackoverflow.com/questions/42494885/fill-boxplot-color-based-on-value-gradient – www Aug 23 '17 at 04:20
  • @Z.Lin, I did, but it created a legend, not solved the problem. – Anakin Skywalker Aug 23 '17 at 05:54
  • @ycw, scale_color_gradient2() did not fix the problem also. – Anakin Skywalker Aug 23 '17 at 05:57
  • 1
    I thought the main point of the post I shared is it is difficult to fill gradient color to boxplots, and it doesn't add new information in terms of visualization. – www Aug 23 '17 at 06:00
  • @ycw, probably it odes not, but it is nicer for sure. If Tableau does it, I cannot believe that R cannot fix it. I tried just to google gradient boxplot and do not see anything. – Anakin Skywalker Aug 23 '17 at 06:15
  • 1
    Folks, relax, Hadleys said no :( Hadley Wickham‏Verified account @hadleywickham 1m 1 minute ago No sorry – Anakin Skywalker Aug 23 '17 at 13:59

1 Answers1

1

I'm not familiar with Tableau, but it looks like that not a gradient per se, but rather that there are points which are colored based on which quartile of the boxplot they're in. That can be done!

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

enter image description here

Brian
  • 7,900
  • 1
  • 27
  • 41