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)
Related
This question already has answers here:
Add mean to grouped box plot in R with ggplot2
(2 answers)
Closed 1 year ago.
I am trying to add the mean values (as shown in red dots in the plot below) in the boxplot with ggplot2. I used stat_summary to add mean values.
However, the following plot is not the exact one that I am looking for. What I'd like to get is to show two mean values for both Y (blue box) and N (red box), not one mean value for both.
Here is my code.
ggplot(data = df.08.long,
aes(x = TMT_signals, y = as.numeric(TMT_Intensities), fill = `probe.Mod.or.not(Y/N)`)) +
geom_boxplot() +
stat_summary(fun.y=mean, geom="point", shape=20, size=5, color="red", fill="red") +
coord_cartesian(
xlim = NULL,
ylim = c(0, 2e4),
expand = TRUE,
default = FALSE,
clip = "on")
theme_classic() +
theme(axis.title=element_text(size=8),
axis.text=element_text(size=10),
axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
Does anyone know how to solve this problem?
Thanks so much for any help!
mtcars example
Code
mtcars %>%
ggplot(aes(as.factor(vs),drat, fill = as.factor(am)))+
geom_boxplot()+
stat_summary(
fun=mean,
geom="point",
shape=21,
size=5,
#Define the aesthetic inside stat_summary
aes(fill = as.factor(am)),
position = position_dodge2(width = .75),
show.legend = FALSE
)
Output
This question already has answers here:
Force the origin to start at 0
(4 answers)
Closed 2 years ago.
I was wondering if there is a way for me to adjust the location where the notches of the axis meet the edge of the bar plot. If you see in the image below all the notches of the x-axis line up in the center of the bar, I would like them to line up with the left edge of the bar! Is this possible?
Edit: Heres what my code looks like
hr6_xy = data.frame(hr6_bins, hr6_Occur)
hr6_plot = ggplot(hr6_xy, aes(hr6_bins, hr6_Occur)) +
geom_bar(stat = "identity",
color = 'black', fill = 'pink', width = 1)
hr6_plot = hr6_plot + theme_bw()
hr6_plot = hr6_plot + ggtitle("hr6 (68 sites)")
hr6_plot = hr6_plot + xlab("Distance to TSS from Motif Center, kb")
hr6_plot = hr6_plot + ylab("Occurances")
hr6_plot
Two ways I can think of:
(a) shift the position of the bars with position_nudge
ggplot(data = iris, aes(y=Sepal.Length, x = Species)) +
geom_bar(stat = "summary", width = 0.8,
position = position_nudge(x = 0.4))
(b) shift the position of the ticks on the x-axis with scale_x_continuous
ggplot(data = iris, aes(y=Sepal.Length, x = as.numeric(Species))) +
geom_bar(stat = "summary", width = 0.8) +
scale_x_continuous(breaks = c(0.6, 1.6, 2.6),
labels = levels(iris$Species))
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.
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"))
I need help to align the data labels for my bar graph in ggplot to the center of each bar just above the bar. Also month in the X axis are repeating. Kindly find my ggplot code for my shiny app below,
ggplot(data = chart_m, aes(x = `Reporting Month`, y = `Total tags generating alert`, fill = HSL)) +
geom_bar(stat = "identity", position = 'dodge') +
geom_text(aes(label = `Total tags generating alert`), position = position_dodge(width = .9), vjust = -.1, size = 6) +
scale_x_date(labels = date_format("%b-%y")) +
theme_grey()
For your reference i have attached the image. I need all the data labels to be aligned to each of the bar in the center.
expected output
Any inputs appreciated.
Regards,
Mohan