Home > OS >  Aesthetics : fill - Warning ocours when ploting gg Boxplot
Aesthetics : fill - Warning ocours when ploting gg Boxplot

Time:10-24

Ive written a R-chunk which should provide me a coloured ggplot boxplot. All needed templates are loaded, so is the Data.

The Data for „Healthy“ & „BodyTemperature“ is based inside the Data „Hospital“. For Healthy there can be only 0 oder 1.

It should plott two Boxplots next to each other on the x-axis, one showing Healthy (0) the other one Unhealthy (1) compared to the BodyTemperature of the patients on y-axis. The Boxplot should be coloured with the Template „Brewer“.

Everytime i try to run this chunk, a warning occours. Whats the solution?

colour:

colour <- brewer.pal(n = 2, name = "Set1")
colour

Warnung: minimal value for n is 3, returning requested palette with 3 different levels [1] "#E41A1C" "#377EB8" "#4DAF4A"

R-Chunk

colour = brewer.pal(n = 2, name = "Set1")

ggplot(Hospital, aes(x = Healthy, y = BodyTemperature))  
geom_boxplot(fill=c(colour))  
  ylab("Temperature")   
  xlab("Healthy")  
  ggtitle("Health compared to Temperature")

Warning ocours:

Error in `check_aesthetics()`:
! Aesthetics must be either length 1 or the same as the data (1): fill
Backtrace:
  1. base (local) `<fn>`(x)
  2. ggplot2:::print.ggplot(x)
  4. ggplot2:::ggplot_build.ggplot(x)
  5. ggplot2 (local) by_layer(function(l, d) l$compute_geom_2(d))
  6. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
  7. l$compute_geom_2(d)
  8. ggplot2 (local) f(..., self = self)
  9. self$geom$use_defaults(data, self$aes_params, modifiers)
 10. ggplot2 (local) f(..., self = self)
 11. ggplot2:::check_aesthetics(params[aes_params], nrow(data))
 Error in check_aesthetics(params[aes_params], nrow(data)) :

CodePudding user response:

As you want to color your boxplots by the value of Healthy you could do so by mapping Healthy on the fill aesthetic. Also to use one of the Brewer palettes ggplot2 already offers some convenience functions which in case of the fill aes is called scale_fill_brewer. Not sure whether you want a legend but IMHO it does not make sense so I removed it via guides. Finally as you provided no data it's not clear whether your Healthy column is a numeric or a categorical variable. For this reason I wrapped in factor to make it categorical.

Using some fake random example data:

set.seed(123)

library(ggplot2)

Hospital <- data.frame(
  Healthy = rep(c(0, 1), 50),
  BodyTemperature = runif(100)
)

ggplot(Hospital, aes(x = factor(Healthy), y = BodyTemperature))  
  geom_boxplot(aes(fill = factor(Healthy)))  
  scale_fill_brewer(palette = "Set1")  
  ylab("Temperature")  
  xlab("Healthy")  
  ggtitle("Health compared to Temperature")  
  guides(fill = "none")

CodePudding user response:

Thanks a lot for the perfect help!

I`ve got another issue with colouring a ggplot2 histogram.

R-Junk

ggplot(Hospital, aes(x=BodyTemperature))  
  geom_histogram(aes(fill = factor(BodyTemperature)))  
  scale_x_continuous(breaks = seq(0, 100, by = 10))  
  ylab("prevalence")   
  xlab("BodyTemperature")  
  ggtitle("Temperature vs. prevalence")

So the histogram should plot the information (x-axis), that as higher the temperature gets, it gets worse. So for example temperature at 36°C should be green, 38°C yellow, 40° red - going from left to right on the x-axis. Y-Axis should provide how often these temperatures ocures in the Patientdata of the Hospital. The Data "BodyTemperature" ist a list of 200 Data like: "35.3" or "37.4" etc.

How can this chunk be fixed to provide the color changes? For a non-ggplot version i aleeady written this r-junk positiv:

```{r, fig.width=8}
color1 <- rep(brewer.pal(1, "Greens"))
color2 <- rep("#57c4fa", 0)
color3 <- brewer.pal(8, "Reds")
hist(Hospital$BodyTemperature,
  breaks = seq(from = 0, to = 100, by = 10),
  main = "Temperature vs. prevalence", 
  ylab = "prevalence",
  xlab = "Temperature", 
  col = c(color1, color2, color3))
```
  • Related