Modifying the legend colour and text in ggplot - R - r

My data set contains three columns, name, value and indicator. I want to plot a bar graph with these three variables. the following is my code,
ggplot(data , aes(x = factor(name), y = value, color = as.factor(indicator))) + geom_bar(stat = "identity" ) +
scale_color_manual(values = c("lightblue", "red"), guide = guide_legend(title = "text", labels = c('A', 'B'))) +
xlab("xlab") + ylab("ylab") + ggtitle("title")
and following is the graph I got,
But the legend here is not showing the lightblue and red. Only the outline is of that colour, but inside it shows only grey. Can anybody help me in filling the legend with the similar color as the graph color. Also I want to change the 0,1 to A and B text.
Can anybody help me in doing this? Any help would be appreciated,
Thanks

The color aesthetic only changes the outline color of bars in ggplot2. To change the color of the entire bar you'll want to use fill instead.
After replacing color with fill in your code, you can change the colors used along with the title and labels of the legend in scale_fill_manual.
scale_fill_manual(values = c("lightblue", "red"), name = "text", labels = c('A', 'B'))

Related

How to change colour of histograms with facet_grid and the position of the text?

How can I change the colour for the two histograms plot made with facet_grid?
I would like to have the histogram of "Active" in green and "Failed" in red.
And also, how can I change the position of the text of the line in order to have it a bit more down?
Here is my code:
df %>%
ggplot(aes(x = `Banks/turnover%Last avail. yr`)) +
geom_histogram(
bins = nclass.Sturges(`Banks/turnover%Last avail. yr`),colour = "black")+
geom_vline(data = status_means, aes(xintercept = avg), color="red", linetype="dashed")+
geom_text(data = status_means , aes(x = avg, label = avg), y= Inf )+
theme(legend.position="None")+
facet_grid(~general_status)
You need to add a factor to the fill parameter. Here, I just use active-failed as I'm not sure how you have that distinguished in your data.
ggplot(df_all,
aes(x = `Banks/turnover%Last avail. yr`, fill = as.factor(active-failed)))
Then, you can add a line to define the colors:
scale_fill_manual(values = c("green", "red"))
As #teunbrand said, you can just use vjust to move the text so that it is not cut off.
Remember to give a good reproducible example, it is helpful to provide some of your data via dput(head(df)).

How can I include two colors in one box of my legend?

I have a bar graph that I made using ggplot2 that is colored both by color= and by fill=, and both of those aesthetics appear in my legend (see picture below). I am happy with how the color part of the legend is appearing, but for the fill part, instead of the boxes being white, I would like there to be a combination of both the red and the yellow, to indicate that the outline can be found on bars of either color. I was hoping that the boxes could be split down the middle diagonally, or something similar, with half of the "legend box" red and half of the "legend box" white. Right now, this is the code that I have for the coloring of my graph:
guides(color = guide_legend(override.aes = list(fill = "white")))
My current plot can be found at the following link:
Again, ideally, those white boxes would each be filed with the two colors, red and yellow, half of each box for each color. I have tried to make a vector of the two colors and assign that to the fill, but that just colors one box red and one box yellow.
A reproducible example, with an example dataset that mimics my own, is included below:
rpe <- data.frame(unit = rep(c(1, 2, 3, 4), times = 2),
value = c(8.33, 43.55, 15.63, 87.59, 91.67, 56.45, 84.37, 12.41),
category1 = (c(rep("Expected", times = 4), rep("Unexpected", times = 4))),
tag = c("AMPH", "APO", "APO", "AMPH", "AMPH", "APO", "APO", "AMPH")
)
ggplot(rpe, aes(unit, value, fill = category1, color = tag)) +
geom_bar(position = "fill", stat = "identity") +
scale_y_continuous(labels = percent) +
scale_fill_manual(values=c("tomato", "darkgoldenrod1")) +
scale_color_manual(values = c("black", NA)) +
guides(color = guide_legend(override.aes = list(fill = "white")))
Any help would be very much appreciated! Thank you!

Define the color of different types in bidirectional bar chart

I'm trying to apply different colors for type "down" and type "up". How can I define the specific color the type for down (blue) and type up (red)? Thank you in advance for any advice!
The following is my data:
category| number| type;
a|-1|down; b|-4|down; a|30|up; b|22|up.
I tried to use scale_fill_manual in my example, but it seems to be used for all single direction bar charts in those examples I could find. I wonder how to use scale_fill_manual for bidirectional bar chart. Below is the code I used. I am fairly new to R. I appreciate any comments.
testData = read.csv("C:/Users/K/Desktop/example001.csv")
library(ggplot2)
library(scales)
ggplot(data = testData,
aes(x = category, y = number,
fill = factor(type))) +
geom_bar(position = "identity", stat = "identity", width = 0.5) +
coord_flip() +
labs(title = "example001") +
ylim(-50, 50)
what I want is blue for downregulation and red for upregulation, not like the colors in the attched image.
enter image description here
As #jon-spring suggested, simply add scale_fill_manual to your ggplot object:
+ scale_fill_manual(values = c("blue","red"))
If you don't want to figure out the order, you can assign specific colours to specific factors:
+ scale_fill_manual(values = c("down"="blue","up"="red"))
You can even use hex codes for very specific colours:
+ scale_fill_manual(values = c("down"="#0433ff","up"="#ff2500"))

geom_ribbon different colours - R

I am using the following code to plot my data but I cannot manage to set the colours to geom_ribbon properly.
My graph contains 4 lines, each of one with a different color. I want the 'geom_ribbon' of each line to have the same color as its line (with transparency - alpha).
In addition, when I change the value of alpha (e.g. from 0.1 to 0.9) I dont't see any change on the transparency. Finally, an extra class is added in the legend and I would like to remove this? Any help on this basic ggplot?
ggplot(dfmean_forplot, aes(x = image, y = value, group = ID)) +
geom_line(aes(colour=factor(ID)))+
scale_x_discrete(breaks=1:21,
labels=c("19/1","7/2","17/2","18/3","17/4","27/4","17/5","27/5","7/6","16/6","26/6","5/7","16/7","6/8","15/8","25/8","4/9","25/9","4/10","14/10","22/11"))+
xlab("# reference")+
ylab("value")+
scale_colour_discrete(name = "class")+
ylim(0,0.9)+
geom_ribbon(aes(ymin=dfmean_forplot$value-dfsd_forplot$value, ymax=dfmean_forplot$value+dfsd_forplot$value, alpha = 0.3))
EDIT
What about the legend? Ideally, I would like to combine them so that there is a square for each color crossed by a line of the same color
You need to add the fill aesthetic and take alpha outside aes, both for geom_ribbon. The following code should solve that.
ggplot(dfmean_forplot, aes(x = image, y = value, group = ID)) +
geom_line(aes(colour=factor(ID)))+
scale_x_discrete(breaks=1:21,
labels=c("19/1","7/2","17/2","18/3","17/4","27/4","17/5","27/5","7/6","16/6","26/6","5/7","16/7","6/8","15/8","25/8","4/9","25/9","4/10","14/10","22/11"))+
xlab("# reference")+
ylab("value")+
scale_colour_discrete(name = "class")+
ylim(0,0.9)+
geom_ribbon(aes(ymin=dfmean_forplot$value-dfsd_forplot$value,
ymax=dfmean_forplot$value+dfsd_forplot$value,
fill = factor(ID)), alpha = 0.3)

ggplot2 colour assignment loses fill and border?

Using ggplot2 to create a boxplot, I ran into the issue of needing to specify legend colour.
Before adjustment each box had a filled centre and black border Before picture here. However, after adjusting legend colour the fill is white and the border is the specified colour. After picture here How would I go about getting my specified colour as the fill with a black border?
ggplot(Data.Benin, aes(x=countrycode, y=mortality, color=year)) +
geom_boxplot(notch=F)+ scale_color_manual(values=c("coral1",
"darkolivegreen4", "gold4", "deepskyblue2", "darkorchid1"))+labs(x=" ",
y="Deltamethrin mortality", fill="Year")+theme_gray()
Thank you in advance.
color changes the border color, what you want is fill:
ggplot(Data.Benin, aes(x = countrycode, y = mortality, fill = year)) +
geom_boxplot(notch = F) +
scale_fill_manual(values = c("coral1", "darkolivegreen4", "gold4", "deepskyblue2", "darkorchid1")) +
labs(x = " ", y = "Deltamethrin mortality", fill = "Year") +
theme_gray()
Note that you correctly had fill = inside labs but the legend title of your plot was the lowercase year (i.e. it had no effect since you didn't set a fill aesthetic).
Also in the future you might want to include data with your question so others can replicate your issue. See how to make a great R reproducible example.

Resources