Reputation: 19
I have a script that creates scatter a scatter plot and two bar charts from my data. Everything works fine except that when i try to change the colors of the scatter plot it does not work and they remain the standard ones.
I want to take the red and gray colors (#f40009 and #8e8f90 respectively) from the bar chart and color the scatter plot with them, but it does not work.
Does anyone have an idea as to why this is happening and how to work it out?
The code for the scatter plot is the following:
lof <- 2
days <- 3
plot2 <-ggplot( data3, aes( CE11000_ERLOS, Z_UCS))+
geom_point(aes(colour=ifelse( data3$LOF>lof & data3$Z_LAST_DAYS<days & data3$CE11000_ERLOS>100,"#8e8f90","#f40009")), size = 3)+
labs(list(y = "Unit cases", x = "Gross sales revenue"))+
ggtitle(bquote(atop(.("Visualization of Outliers"), atop(italic(.(country)), ""))))+
scale_colour_discrete(guide = guide_legend(title = NULL), labels = c("Outliers", "Not outliers"))+
theme(plot.title = element_text(hjust = 0.5))+
geom_text(aes(label = ifelse( data3$LOF>lof & data3$Z_LAST_DAYS<days,paste( data3$Z_CDOW, data3$CE11000_BUDAT2,sep = "\n "),""),hjust=1.05,vjust=1), size = 3.5)+
scale_y_continuous(labels = comma)+
scale_x_continuous(labels = comma)
print(plot2)
The data is:
LOF Z_LAST_DAYS CE11000_ERLOS Z_UCS Z_CDOW CE11000_BUDAT2
3.1 1 996789 21195 Thursday 20170126
1.01 23 11912948 210839 Wednesday 20170104
1.4 22 14322767 257269 Thursday 20170105
1.01 21 11817447 185197 Friday 20170106
1.66 18 7906971 153807 Monday 20170109
And this is the result scatter
Upvotes: 0
Views: 3551
Reputation:
Sample code to change default colors in ggplot2
data <- iris[iris$Species == "setosa" | iris$Species == "virginica", c(1:2,5)]
levels(data$Species)
droplevels(data$Species)
data
plot <- ggplot(data, aes(x = Sepal.Length, y = Sepal.Width)) +
geom_point(aes(color = Species))
The below plot generates default colors
plot
You can add scale_color_manual()
to change the default colors
plot + scale_color_manual(
values=c("setosa" = "darkgrey","virginica" = "red"))
Upvotes: 1
Reputation: 1134
in this line
geom_point(aes(colour=ifelse( data3$LOF>lof & data3$Z_LAST_DAYS<days & data3$CE11000_ERLOS>100,"#8e8f90","#f40009")), size = 3)
you're mapping the levels of what you put in the colour statement to (the default) colours by putting them inside the aes
call.
If you take your colour statement out of the aes
call you should get the actual colours.
compare e.g.:
not pink:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point(aes(colour="pink"))
pink:
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point(colour="pink")
or, in your case:
geom_point(colour=ifelse( data3$LOF>lof & data3$Z_LAST_DAYS<days & data3$CE11000_ERLOS>100,"#8e8f90","#f40009"), size = 3)
alternatively, you can change the default colours using scale_colour_manual
as per the comment by @Adam Quek.
Upvotes: 1