0

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

1 Answers1

0

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

stefan
  • 90,330
  • 6
  • 25
  • 51
  • Hi Stefan, ive seen your from Germany so it would be easy to change: – Harry Plotter Oct 23 '22 at 17:53
  • Herzlichen Dank für die schnelle Antwort!! – Harry Plotter Oct 23 '22 at 17:53
  • Ich habe für "Healthy" eine Spalte in der Datentabelle, bestehend aus 0 und 1. Wofür stehen diese Ausrücke? set.seed(123) library(ggplot2) Hospital <- data.frame( Healthy = rep(c(0, 1), 50), BodyTemperature = runif(100) ) Es hat aufjeden Fall schon funktioniert. Ich habe das identische Problem noch bei einem gg Histogram - ich werde es gleich ausprobieren! – Harry Plotter Oct 23 '22 at 17:55
  • Gerne. Der Ausdruck erstellt einen Beispieldatensatz mit 50 0en und 1en für die Variable `Healthy` und 100 Zufallswerten aus einer Gleichverteilung für die Variable `BodyTemperature`. `set.seed(123)` stellt sicher, dass du oder jede:r andere die gleichen "Zufallswerte" erhält. 123 ist dabei beliebig. Auch ein genereller Hinweis für die Zukunft: Stets einen Beispieldatensatz oder einen Teil deines Datensatzes zur Verfügung stellen. Siehe hierzu [How to make great reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – stefan Oct 23 '22 at 18:07
  • Und auch wenn wir beide auf Deutsch kommunizieren können: Besser auf Englisch, damit auch andere die Hinweise und Kommentare lesen und verstehen können. (: – stefan Oct 23 '22 at 18:09
  • Okay vielen Dank - ich arbeite am Histogramm — ich antworte nachher auf diese Nachricht in english – Harry Plotter Oct 23 '22 at 18:32
  • If you feel more comfortable in German, may I propose the German R forum at forum.r-statistik.de ? – Bernhard Oct 23 '22 at 18:38