This question already has answers here:
Remove space between plotted data and the axes
(3 answers)
Force the origin to start at 0
(4 answers)
Closed 5 years ago.
I have a data frame that looks like this:
df = data.frame(
comp_id= c("1A","1A","1A"),
rate = c(93,93,93),
quartile = c("25 pctl","50","75 pctl"),
quartile.value = c(88,92,95)
)
It graphs a point against quartiles. For some reason, I cannot remove the gap between the bar and the left axis.
-Setting cartesian doesn't work, and adding expand(0,0) to scale_y_continuous also doesn't work
library(scales)
ggplot(df,
aes(x = comp_id)) +
geom_col(aes(y = quartile.value, fill = quartile),
position = position_dodge(0), width = 2.5) + scale_color_manual(values=c("black"),labels=c("comp_id")) +
geom_point(aes(y = rate, color="comp_id"), size=3, shape=20) +
scale_fill_manual(values = c("azure2", "azure3", "azure4"), labels=c("25th Pctl","Median","75th Pctl")) +
labs(x="", y="") + scale_y_continuous(limits=c(80,100), oob=rescale_none) + theme(panel.border = element_blank(), panel.background=element_blank()) +
theme(legend.title=element_blank()) + theme(legend.position="bottom") + scale_x_discrete(expand=c(0,0))
Edit: I used the scales package because the bars disappeared when I specified the limits on the y axis.
Related
This question already has an answer here:
Position geom_text on dodged barplot
(1 answer)
Closed 1 year ago.
How do I adjust the position in geom_text to distribute the column labels over each of their respective columns, currently they are all aligned on top of each other (see picture)
ggplot(ESCd, aes(factor(S),fill = ESC9)) +
geom_bar(stat="count", position = "dodge")+
geom_text(stat='count', aes(label=..count..), vjust=-1, position = "identity")
We could use position = position_dodge():
Here is an example with the built in diamonds dataframe:
library(ggplot2)
ggplot(diamonds, aes(factor(cut),fill = factor(color))) +
geom_bar(stat="count", position = "dodge")+
geom_text(stat='count', aes(label=..count..), position = position_dodge(width = 0.9),
vjust = -0.25, color = "black", size = 3)
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:
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:
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"))
This question already has an answer here:
ggplot2 plot area margins?
(1 answer)
Closed 5 years ago.
How can I increase the area around a plot area in ggplot 2 to give my axis titles some breathing room. I am aware of vjust and hjust (as below), however, I can't seem to create actual space around the plotting area to move my axes titles onto.
p <- ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point()
p
p<- p + theme(axis.title.x = element_text(family="Times",size=20,face="bold",colour = "Black",vjust=-1,hjust=0.5))
p
Margins around plot can be modified with theme(), plot.margin = and function margin() where you provide size of margins starting with top, then right, bottom and left, and units (default is "pt").
ggplot(mtcars, aes(x = wt, y = mpg)) + geom_point() +
theme(axis.title.x = element_text(family = "Times",size = 20,
face = "bold",colour = "Black",vjust = -1,hjust = 0.5))+
theme(plot.margin = margin(1,1,1.5,1.2, "cm"))