Reputation: 7643
I am new to R programming.
Stuck while plotting ggplot violin plot.
I wish to give color blue
, green
and red
color to my 3 groups.
Here is my dataframe:
> print(employee_withoutEmployeeID[1:5,])
>
EnvironmentSatisfaction JobSatisfaction WorkLifeBalance
1 3 4 2
2 3 2 4
3 2 2 1
4 4 4 3
5 4 1 3
What I am trying is:
png(file="answer5.png")
answer5 <- ggplot(stack(employee_withoutEmployeeID), aes(x = ind, y= values) )
answer5 + geom_violin() + scale_fill_manual(values=c("#0000FF", "#00FF00", "#FF0000")) +
labs(title="Answer 5",
subtitle="",
caption="Answer 5",
x="Measure",
y="Rating")
dev.off()
I am getting my violin plot plotted correctly, but there is no color fill. I am not sure, how and where to use scale_fill_manual
Please suggest
Upvotes: 1
Views: 3779
Reputation: 24252
The fill
aestetic needs to be specified in geom_violin
:
library(ggplot2)
ggplot(iris, aes(x= Species, y = Sepal.Width) ) +
geom_violin(aes(fill= Species)) +
scale_color_manual(values=c("#0000FF", "#00FF00", "#FF0000")) +
labs(title="Answer 5", subtitle="", caption="Answer 5",
x="Measure", y="Rating")
Upvotes: 2
Reputation: 24079
You need to define the variable to fill on. The stack function will add the ind
column onto your input data.frame, thus I added the fill=ind
into the aes option.
answer5 <- ggplot(stack(employee_withoutEmployeeID), aes(x = ind, y= values, fill=ind) )
answer5 + geom_violin() + scale_fill_manual(values=c("#0000FF", "#00FF00", "#FF0000")) +
labs(title="Answer 5",
subtitle="",
caption="Answer 5",
x="Measure",
y="Rating")
Upvotes: 2