Rename Legend title on facet_wrap [duplicate] - r

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

Related

How to recreate this plot in ggplot2? [duplicate]

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")

ploting vectors with ggplot2, adding legend [duplicate]

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")

Plotting multiple Pie Charts with label in one plot

I came across this question the other day and tried to re-create it for myself. ggplot, facet, piechart: placing text in the middle of pie chart slices
. My data is in a very similar format, but sadly the accepted answer did not help, hence why I am re posting.
I essentially want to create the accepted answer but with my own data, yet the issue I run into is that coord_polar does not support free scale. Using the first answer:
I tried it using the second version of the answer, with the ddplyr version, but I also do not get my desired output. Using the second answer:
Clearly none of these has the desired effect. I would prefer to create one as with size pie charts, but only showed four as an example, follows: .
This I did in excel, but with one legend, and no background grid.
Code
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
library(tidyverse)
p1<-ggplot(data = piec, aes(x = "", y = value, fill = type)) +
geom_bar(stat = "identity") +
geom_text(aes(label = value), position = position_stack(vjust = 0.5)) +
coord_polar(theta = "y")
#facet_grid(title ~ ., scales = "free")
p1
piec <- piec %>% group_by(title) %>% mutate(pos=cumsum(value)-0.5*value)
p2<-ggplot(data = piec) +
geom_bar(aes(x = "", y = value, fill = type), stat = "identity") +
geom_text(aes(x = "", y = pos, label = value)) +
coord_polar(theta = "y")
#facet_grid(Channel ~ ., scales = "free")
p2
You don't have to supply different y values for geom_text and geom_bar (use y = value for both of them). Next you have to specify position in geom_text. Finally, remove scales from facets.
library(ggplot2)
title<-c(1,1,2,2,3,3,4,4,5,5,6,6)
type<-c('A','B','A','B','A','B','A','B','A','B','A','B')
value<-c(0.25,0.75,0.3,0.7,0.4,0.6,0.5,0.5,0.1,0.9,0.15,0.85)
piec<-data.frame(title,type,value)
ggplot(piec, aes("", value, fill = type)) +
geom_bar(stat = "identity", color = "white", size = 1) +
geom_text(aes(label = paste0(value * 100, "%")),
position = position_stack(vjust = 0.5),
color = "white", size = 3) +
coord_polar(theta = "y") +
facet_wrap(~ title, ncol = 3) +
scale_fill_manual(values = c("#0048cc", "#cc8400")) +
theme_void()

Second Y-Axis in ggplot (R) [duplicate]

This question already has answers here:
ggplot with 2 y axes on each side and different scales
(18 answers)
Closed 4 years ago.
I am trying to make a plot with ggplot in a Shiny app in R and I need to set a second Y-axis in it. This plot has two types of graphics: lines and bars. I would like to represent the bars (depth of precipitation) on the left, and the lines (flows) on the right.
My current code is:
output$plotRout <- renderPlot({
ggplot(totalRR(),aes(x=time)) +
geom_bar(aes(y=mm), stat = "identity",fill = "dodgerblue",color = "black") +
geom_bar(aes(y=NetRain), stat = "identity",fill = "Cyan",color = "black") +
geom_line(aes(y=DirRun, colour = "Direct Runoff"), stat = "identity",color = "Red") +
geom_line(aes(y=BF, colour = "Baseflow"), stat = "identity",color = "Darkorange", linetype = "longdash") +
scale_y_continuous("Rainfall (mm)", sec.axis = sec_axis(~.*10, name = "Flow (m3/s)")) +
xlab("Time (h)")
})
The result is:
This plot has on the left the values of the flows, the values that should be on the right, whereas the values of rainfall (the bars) are not displayed on the plot.
How could I make this plot putting the values of the bars (rainfall) on the left and the second y-axis on the right showing the values of the lines (flows)?
Many thanks in advance.
Victor
One solution would be to make the Flow axis your primary y axis. This involves 1) scaling the data using *10 and then 2) transforming the secondary axis using /10 to get back the correct numbers for the Rainfall axis:
ggplot(totalRR(),aes(x=time)) +
geom_bar(aes(y=10*mm), stat = "identity",fill = "dodgerblue",color = "black") +
geom_bar(aes(y=10*NetRain), stat = "identity",fill = "Cyan",color = "black") +
geom_line(aes(y=10*DirRun, colour = "Direct Runoff"), stat = "identity",color = "Red") +
geom_line(aes(y=10*BF, colour = "Baseflow"), stat = "identity",color = "Darkorange", linetype = "longdash") +
scale_y_continuous("Flow (m3/s)", sec.axis = sec_axis(~./10, name = "Rainfall (mm)")) +
xlab("Time (h)")

Removing "a" symbol from colour legend in ggplot2 [duplicate]

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)

Resources