I want to change the color of ggplot bar charts - r

I want to change the color of ggplot bar charts manually using the scale_color_manual function. Here is the code:
library(ggplot2)
ggplot(UM.Leads, aes(Leads, Count, fill = Model)) +
geom_bar(stat = "identity") +
xlab("Electrode Model") +
ylab("DBS Leads") +
ggtitle("University of Minnesota") +
scale_color_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()
I cannot seem to change the fill of the bar graphs from the default pink, green, and blue that ggplot provides. Any help would be much appreciated!
See plot here: http://rpubs.com/Gopher16/393415

To illustrate the comment from #Jack Brookes and create a reproducible example:
library(ggplot2)
df <- data.frame(
gp = factor(rep(letters[1:3], each = 10)),
y = rnorm(30)
)
ggplot(df, aes(gp, y, fill=gp)) +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("darkgoldenrod1", "grey55", "dodgerblue1")) +
theme_classic()

Related

How to flip a geom_area to be under the line when using scale_y_reverse()

I had to flip the axis of my line, but still need the geom_area to be under the curve. However I cannot figure out how to do so.
This is the line of code I tried
ggplot(PalmBeachWell, aes(x=Date, y=Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_area(position= "identity", fill='lightblue') +
theme_classic() +
geom_line(color="blue") +
scale_y_reverse()
and here is what i got
One option would be to use a geom_ribbon to fill the area above the curve which after applying scale_y_reverse will result in a fill under the curve.
Using some fake example data based on the ggplot2::economics dataset:
library(ggplot2)
PalmBeachWell <- economics[c("date", "psavert")]
names(PalmBeachWell) <- c("Date", "Depth.to.Water.Below.Land.Surface.in.ft.")
ggplot(PalmBeachWell, aes(x = Date, y = Depth.to.Water.Below.Land.Surface.in.ft.)) +
geom_ribbon(aes(ymin = Depth.to.Water.Below.Land.Surface.in.ft., ymax = Inf),
fill = "lightblue"
) +
geom_line(color = "blue") +
scale_y_reverse() +
theme_classic()

Bar plot options with ggplot2

I am actually trying to do a graph with ggplot2 but I'd like to add some options (colors, legend...).
Here is my code :
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment)) +
stat_summary(fun.y="mean", geom="bar") +
facet_grid(. ~ treatment) +
theme_grey() +
xlab("Treatment") +
ylab("OT") +
scale_fill_grey() +
theme(strip.background = element_rect(colour = "black", fill = "white"))
And here the actual output.
Could you please indicate me how to change the name of 1 and 2 (without changing in it the dataframe) and how to add colours to this ?
I tried this
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, colour=Treatment))
But it applies the color only to the outline of the figure.
To change the color of the bars you need fill = Treatment.
To change the labels on the x axis you need scale_x_discrete(labels = your_labels). See here.
So your code will look like:
ggplot(FINAL, aes(x = as.factor(gender), y=overtreatment, fill= Treatment)) +
scale_x_discrete(labels = your_labels) +
...

manual color assignment of a legend object in ggplot2

This is my code:
library(data.table)
library(ggplot2)
clist <- list(c(1:39), c(2:40), c(3:41))
clist.ts <- lapply(clist, function(x) ts(x, frequency = 1, start = 1978))
ind78_ymean_tsdf <- as.data.frame(clist.ts)
names(ind78_ymean_tsdf) <- c("name1", "name2", "name3")
ind78_ymean_tsdf$"Year" <- c(1978:2016)
setDT(ind78_ymean_tsdf)
ind78_ymean_melt <- melt(ind78_ymean_tsdf, id=c("Year"))
(ggplot(ind78_ymean_melt, aes(x=Year, y=value, color=variable))
+ geom_line()
+ geom_line(data=subset(ind78_ymean_melt, variable == "name1"), colour="black", size=1.5)
+ labs(title="Development of the indices", x="Year", y="Index")
+ scale_color_discrete(name="Individual replications")
+ theme_light())
# + guides(colour=guide_legend(override.aes=list(colour=c(hue_pal()(11)[1:10], "black"), size=c(rep(1, 10), 1.5))))
It is basically the same as in the following question, but with a reproducible example: manual color assignment
My problem is now, that I don't know how I had to change the following line in order to get the entry in the legend of the plot also black:
guides(colour=guide_legend(override.aes=list( colour=c(hue_pal()(11)[1:10], "black"), size=c(rep(1, 10), 1.5))))
Maybe someone could explain what the parameters in the line above mean or could post the question to the link above, because I have not enough street cred. to do so.. :) I have 13 variables in the real plot (not in the reproducible example above) if that helps. Thanks in advance!
Your solution looks overcomplicated. Why don't you simply scale_color_manual and scale_size_manual.
ggplot(ind78_ymean_melt, aes(Year, value, color = variable, size = variable)) +
geom_line() +
labs(title = "Development of the indices",
x = "Year",
y = "Index",
color = "Individual replications") +
scale_color_manual(values = c("black", hue_pal()(2))) +
scale_size_manual(values = c(1.5, rep(0.5, 2))) +
theme_light() +
guides(size = FALSE)
This is the solution from #drmariod:
(ggplot(ind78_ymean_melt, aes(x=Year, y=value, color=variable), size=2)
+ geom_line()
+ geom_line(data=subset(ind78_ymean_melt, variable == "name3"), colour="black", size=1.5)
+ labs(title="Development of the indices", x="Year", y="Index")
+ scale_color_discrete(name="Individual replications")
+ theme_light()
+ guides(colour=guide_legend(override.aes=list( colour=c(hue_pal()(2), "black"), size=c(rep(1, 2), 1.0)))) )
And the explanation of the last code line: the hue_pal()(11) creates 11 colours from the hue palette but it chooses only color 1:10 and adds a black color to it.

Add Legend and change Colour on grouped bar plot

I created a plot like this;
library("ggplot2")
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = color, y = ..prop.., group = 2)) +
scale_y_continuous(labels=scales::percent) +
facet_grid(~cut)
Now I want to add a legend for the variable "color", also I want to change the colour of the bars. The graph is exactly how I want it to be, and if possible I don't want change the structure of the dataset, just add a legend and change colours.
I could not find example that fit for this "percentage"-style graphics.
ggplot(data = diamonds, aes(x = color, y = ..prop.., group = cut)) +
geom_bar(aes(fill = factor(..x.., labels = LETTERS[seq(from = 4, to = 10 )]))) +
labs(fill = "color") +
scale_y_continuous(labels = scales::percent) +
facet_grid(~ cut)

Defining specific fill colours on barcharts in ggplot [duplicate]

Currently the default colors for the below bar chart are Apct = Red , BPct = Green, CPct = Blue.
How do I change the color so that Apct = Purple, BPct = Black, CPct = Red?
Here is the working code:
library(reshape)
Time<-data.frame(Time = c(as.POSIXct("2015-02-05 14:35:00"), as.POSIXct("2015-02-05 14:40:00")))
APct<-c(.1,.2,.7)
BPct<-c(.2,.4,.4)
Time
data<-cbind(Time, rbind(APct, BPct))
data
colnames(data)<-c("Time", "APct","BPct","CPct")
rownames(data)<- NULL
data
data<-melt(data, measure.vars =c("APct","BPct","CPct") )
ggplot(data=data, aes( x = Time, y=value, fill=variable)) +
geom_bar(stat="identity") +
ggtitle("% ") + xlab("Time") +
ylab("Percentage") +theme(axis.text.x = element_text(angle = 90, hjust = 1))
Also -
How do I know that purple will go with Apct? Is it alphabetically ordered or can I manaually set something liek Apct = Purple?
Also is there a way to make it so the Cpct is always the bottom bar, BPCt the middle bar and APct the top bar?
Thank you.
you can use scale_fill_manual to control the colours.
So your call to ggplot becomes
ggplot(data=data, aes( x = Time, y=value, fill=variable)) +
geom_bar(stat="identity") +
ggtitle("% ") + xlab("Time") +
ylab("Percentage") +theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_fill_manual(values=c("purple","black","red"))

Resources