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)
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:
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")
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)")
This question already has answers here:
Add legend to ggplot2 line plot
(4 answers)
Closed 2 years ago.
I use ggplot to scatterplot 2 datasets and want to show the legend in the top left. I tried some code but didn't work. I am not sure why this happened.
ggplot(mf, aes(log10(mf[,2]),mf[,1]))
+ ggtitle("Plot")
+ geom_point(color = "blue") + theme(plot.margin = unit(c(1,2,1,1), "cm"))
+ xlab("xxx") + ylab("yyy")
+ theme(plot.title = element_text(size=18,hjust = 0.5, vjust=4))
+ geom_point(data=mf2,aes(log10(mf2[,2]),mf2[,1]),color="red")
+ theme(axis.title.x = element_text(size = rel(1.3)))
+ theme(axis.title.y = element_text(size = rel(1.3)))
+ scale_color_discrete(name = "Dataset",labels = c("Dataset 1", "Dataset 2"))
Since values were not provided, I have used my own values for the demonstration purpose.
mf is a dataframe with log and val as it's column.
You need to put the color parameter inside the aesthetics. This will result in the mapping of colors for the legend. After that you can manually scale the color to get any color you desire.
you can use the below code to get the desired result.
ggplot(mf, aes(val,log))+
geom_point(aes(color = "Dataset1"))+
geom_point(data=mf2,aes(color="Dataset2"))+
labs(colour="Datasets",x="xxx",y="yyy")+
theme(legend.position = c(0, 1),legend.justification = c(0, 1))+
scale_color_manual(values = c("blue","red"))