1

I'm trying to create a histogram with ggplot. Therefore I used the following code:

data_csv <- read.csv("XXX.csv", na="NA")
data_csv$Success <- factor(data_csv$Success)

ggplot(data_csv, aes(x=Phasenew, color=Success)) + 
geom_histogram(binwidth = 1, color="white", fill="steelblue", position = "dodge")

Unfortunatly, the plot lookes like this: enter image description here

What I'm expecting is to have 2 dodged bars next to each other, because I'm using color=Success in the ggplot.

Do you have any idea what I'm doing wrong?

JFR
  • 11
  • 2
  • Don't put quotes around variables in `aes()` or they'll be interpreted as fixed values. – alistaire May 27 '21 at 22:51
  • Removed that. Result is the same – JFR May 27 '21 at 23:04
  • 2
    At a glance `color` should be `fill`, but if you want an answer you need [to provide some data](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – alistaire May 27 '21 at 23:22
  • 1
    Try with removing `color="white", fill="steelblue"` from geom_histogram. You thereby overwrite the `color` aesthetic (which should be `fill` as already mentioned by @alistaire). – stefan May 27 '21 at 23:29
  • It would be easier to help if you create a small reproducible example along with expected output. Read about [how to give a reproducible example](http://stackoverflow.com/questions/5963269). – Ronak Shah May 28 '21 at 05:56

1 Answers1

0

Users @alistaire and @stefan have both essentially provided the answer to your problem, but if it isn't clear, hopefully this will help:

library(tidyverse)
library(palmerpenguins)

penguins$sex <- factor(penguins$sex)
ggplot(penguins, aes(x = bill_length_mm)) +
  geom_histogram(binwidth = 1, color="white", fill="steelblue", position = "dodge")

example_1.png

ggplot(penguins, aes(x = bill_length_mm, fill = sex)) +
  geom_histogram(binwidth = 1, position = "dodge") +
  scale_fill_manual(values = c("skyblue", "dodgerblue"))

example_2.png

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46