aes Problem with ggplot

Hello folks, I am really new and just about to lear how to use r for my thesis for data visualization.

I have the following Problem:
I've got an Excel Chart with a column with three Patient Groups 1,2 and 3 --> 1 = 17x, 2= 15x, 3= 60x
I want to generate a bar chart showing the three groups with different colors.
This is the code:

Patientengruppe <- factor(Gesamt$Patientengruppe)
ggplot(Gesamt, aes(x=Patientengruppe, color = Patientengruppe, fill= Patientengruppe))+
geom_bar(fill = "steelblue")+
labs(x="Patientengruppe", y = "ANzahl der Patienten")+
ggtitle("Aufteilung der Patienten in Patientengruppen")

Now I've got the problem, that I can't change the colors of the single bars. All if them are steel blue. I figured, that there is probably a problem with the group Patientengruppe/ aes but I don't know how to extract the groups from the column to work on the bars separately and add them into one chart.

Hi Jann0sch,

Is this what you're looking for?

You might also find this blog very helpful:

I don't think so
Thats the strucure of my table:
Group
Pat1 1
Pat2 2
Pat3 3
Pat4 2
Pat 5 1
Pat 6 3

My x lab is the group --> 1,2,3
And the y lab has to be the number of patients --> 17,15,60

I think, that my struggle is that x is not dependent on y all the examples are like date connected with temperature or time with speed but I just want to show my groups with the number of patient for each group.

Looking at your code, I noticed that you have defined fill option twice, where the fill = "steelblue" in the geom_bar is placed on top of the fill= Patientengruppe..

So, you should only assign the fill = Patientengruppe in the ggplot(aes(..)) or in the geom_bar(aes(..))

HTH

Thanks for your reply. I After I changed that my bars are all grey. Is there a way to change the color of the separate bar?

Hi!

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

Thats my code

ggplot(Gesamt, aes(x=Patientengruppe, color = Patientengruppe, fill= Patientengruppe))+
geom_bar()+
labs(x="Patientengruppe", y = "ANzahl der Patienten")+
ggtitle("Aufteilung der Patienten in Patientengruppen")

These are the first ten of my patients copied from a csv file I exported

"","Patientengruppe"
"1",2
"2",2
"3",2
"4",2
"5",2
"6",2
"7",2
"8",2
"9",2
"10",2

I want to color the bars separately to show the different groups

An example.

library(tidyverse)

Gesamt <- tibble(
 Patientengruppe = c("1", "1", "1", "2", "2", "3", "3", "3", "3", "3", "3", "2", "1", "1", "3", "3")
)

ggplot(Gesamt, aes(x = Patientengruppe, color = Patientengruppe, fill = Patientengruppe)) +
 geom_bar() +
 labs(x = "Patientengruppe",
      y = "ANzahl der Patienten",
      title = "Aufteilung der Patienten in Patientengruppen")

1 Like

Thanks! Thats look a lot better already. But how do I change my code to get the result you posted? Or is my code in general wrong?

I don't have your data, so I don't know!
Try to code a solution, then share it here, even if it does not work :slightly_smiling_face: