This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 4 years ago.
I am trying to understand ggplot2 and I tried this code:
a=as.data.frame(c(2007:2016))
str(a)
b=runif(10, 1000, 2000) #vector
c=runif(10, 500,1000) #vector
ggplot(data=a, aes(x=a)) +
geom_bar(aes(y=b), stat = "identity") +
geom_line(aes(y=c), color="white", size=0.75)
that gave me this:
Why is there no legend and how can I have one?
You need to specify your fills and colors, then a scale_color_manual and some theme setup.
ggplot(data=a, aes(x=a$`c(2007:2016)`)) +
geom_bar(aes(y = b, fill = "b"), stat = "identity") +
geom_line(aes(y = c, group = 1, color = "c"), size = 0.75) +
scale_colour_manual(labels = "Line", values=c("c" = "white"))+
scale_fill_manual(labels = "Bar",values="grey")+
theme(legend.key=element_rect(fill = "grey"),
legend.title=element_blank()) +
labs(x = "Date", y = "Value")
Related
This question already has answers here:
How to change legend title in ggplot
(14 answers)
Closed 1 year ago.
I have a facet wrap of 8 plots and I want to rename the Legend, it currently defaults to the fill value Depth_bins but I want to be able to change that. I've tried various methods, including the below using scale_color_discrete but nothing changes the title. I'm assuming this is because it's a facet_wrap.
facet_8 <- ggplot(am_dives, aes(x = st_hr, fill = Depth_bins)) +
geom_density(aes(y = ..count../sum(..count..)), position = "stack", alpha = 0.7) +
scale_x_datetime(date_labels = "%H:%M") +
labs(x = "Start hour", y = "Density") +
facet_wrap(~ID, dir = "v", scales = "free_y") +
scale_color_discrete(name = "Depth Bins")
facet_8
any help much appreciated!
I think the labs function should work as mentioned here:
How to change legend title in ggplot
So, something like this:
facet_8 <- ggplot(am_dives, aes(x = st_hr, fill = Depth_bins)) +
geom_density(aes(y = ..count../sum(..count..)), position = "stack", alpha = 0.7) +
scale_x_datetime(date_labels = "%H:%M") +
labs(x = "Start hour", y = "Density") +
facet_wrap(~ID, dir = "v", scales = "free_y") +
labs(fill = "Your Title Here")
facet_8
This question already has an answer here:
Barplot in ggplot
(1 answer)
Closed 1 year ago.
Here is my data and original plot:
z <- dbinom(0:6, size=6, p=0.512)
names(z) <- as.character(0:6)
barplot(z, space=0, ylab="Probability", col = "firebrick", las = 1, xlab = "Number of boys")
I need to recreate this same plot in ggplot2 but I'm struggling to make it look remotely similar. Any help would be appreciated.
Building on your code:
library(ggplot2)
ggplot(data.frame(z = z, num_boys = names(z)), aes(x = num_boys, y = z)) +
geom_bar(stat = "identity", fill = "firebrick", col = "black", width = 1) +
labs(y = "Probability", x = "Number of boys") +
ggthemes::theme_base()
NOTE: I used ggthemes::theme_base() to make the plot look like the base plot your original code produces.
If you want to add some customization of axis and background :
z=as.data.frame(z)
colnames(z)=c("Probability")
z$`Number of boys`=rownames(z)
my_theme= list(theme_bw(),
theme(panel.border=element_blank(),
panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),axis.ticks.x = element_blank(),axis.line.x=element_blank()))
ggplot(z, aes(`Number of boys`,Probability)) +
geom_bar(stat = "identity",width = 1,color="black",fill="firebrick")+
my_theme +
annotate(x=0,xend=0,y=0, yend=0.4, colour="black", lwd=0.75, geom="segment")
This question already has answers here:
R ggplot2 - How do I specify out of bounds values' colour
(2 answers)
asymmetric color distribution in scale_gradient2?
(1 answer)
Closed 4 years ago.
I followed this link, trying to
make 0 as white
values more than 2 the same colour
The code below
library(reshape2)
library(ggplot2)
library(scales)
ran <- matrix(nrow = 3, ncol = 2, c(-2,-1,0,1,2,3))
ran_melt <- melt(ran)
ggplot(ran_melt, aes(Var1, Var2)) +
geom_tile(aes(fill = value), color = "white") +
scale_fill_gradientn(colours = c("red", "white", "blue"),
values = rescale(c(min(ran_melt$value), 0, max(ran_melt$value)))) +
labs(fill = 'legend')
will plot this
If I change max(ran_melt$value) to 2:
ggplot(ran_melt, aes(Var1, Var2)) +
geom_tile(aes(fill = value), color = "white") +
scale_fill_gradientn(colours = c("red", "white", "blue"),
values = rescale(c(min(ran_melt$value), 0, 2))) +
labs(fill = 'legend')
I got this:
So how can I achieve my two goals?
You can use arguments limits and oob in scale_fill_gradientn to achieve what you're after:
ggplot(ran_melt, aes(Var1, Var2)) +
geom_tile(aes(fill = value), color = "white") +
scale_fill_gradientn(
colours = c("red", "white", "blue"),
limits = c(-2, 2),
oob = squish) +
labs(fill = 'legend')
Explanation: oob = squish gives values that lie outside of limits the same colour/fill value as the min/max of limits. See e.g. ?scale_fill_gradientn for details on oob.
Update
If you have asymmetric limits you can use argument values with rescale:
ggplot(ran_melt, aes(Var1, Var2)) +
geom_tile(aes(fill = value), color = "white") +
scale_fill_gradientn(
colours = c("red", "white", "blue"),
limits = c(-1, 2),
values = rescale(c(-1, 0, 2)),
oob = squish) +
labs(fill = 'legend')
This question already has answers here:
Remove 'a' from legend when using aesthetics and geom_text
(6 answers)
Closed 5 years ago.
I keep getting this a on my colour legend when I make this graph in GGPLOT2.
ggplot(sher_ei_si, aes(SI, EI, shape = crop, label = treatment, colour =
management)) +
geom_point() +
geom_text_repel(aes(SI, EI)) +
xlim(0, 100) +
ylim(0, 100) +
labs(x = "Structure", y = "Enrichment", shape = "Crop", colour =
"Management") +
geom_vline(xintercept = 50) +
geom_hline(yintercept = 50) +
scale_colour_manual(values = c("grey0", "grey60")
Plot showing a under colour legend
For exact output generation, please provide the input data.
You can use show.legend = FALSE to exclude the a symbol from your example:
geom_text_repel(aes(SI, EI), show.legend = FALSE)
This question already has answers here:
ggplot, facet, piechart: placing text in the middle of pie chart slices
(4 answers)
R + ggplot2 => add labels on facet pie chart [duplicate]
(1 answer)
Closed 6 years ago.
I am trying to draw a pie chart with ggplot2. My code is shown below.
df <- data.frame(
variable = c("australia","hungary","germany","france","canada"),
value = c(632,20,491,991,20)
)
ggplot(df, aes(x = "", y = value, fill = variable)) +
geom_bar(width = 1, stat = "identity") +
scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
coord_polar(theta = "y") +
labs(title = "pie chart")
I would like to display percentage values. How can I do that?
Try
df <- data.frame(
variable = c("australia","hungary","germany","france","canada"),
value = c(632,20,491,991,20)
)
library(ggplot2)
ggplot(transform(transform(df, value=value/sum(value)), labPos=cumsum(value)-value/2),
aes(x="", y = value, fill = variable)) +
geom_bar(width = 1, stat = "identity") +
scale_fill_manual(values = c("red", "yellow","blue", "green", "cyan")) +
coord_polar(theta = "y") +
labs(title = "pie chart") +
geom_text(aes(y=labPos, label=scales::percent(value)))