I have a histogram plotted in R with the code shown below. I am trying to do 2 things:
How to show percent[%] above each bars?
Add a line plot on the top of existing histogram. That shows the percent[%] accumulation from left to right. For example, see attached figure as an example. The line plot starts at 12.5% then add the next bar (~22.92%) to 12.5%. So, it would plot at ~35.42%. It will add each bar % as its goes from left to right. Is there a way to make a similar line plot on my existing histogram chart in R?
Any help or guidance would be very much appreciated. Thanks!
library(tidyverse)
HoursfromSLA <- c("-100","-100","-100","-100","-100","-100","-100","-100","-100","-100","-100","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-80","-50","-50","-50","-50","-50","-50","-50","-50","-50","-50","-20","-20","-20","-20","-20","-20","-20","-20","-20","-20","20","20","20","20","50","50","50","50","50","50","50","50","75","75","75","75","75","75","100","100","100","100","135","135","135","135","225","225","225","225","310","310","350","350","400","400","500","500","500","500","675","675")
data <- data.frame(HoursfromSLA)
data$group <- ifelse(data$HoursfromSLA<0, "Green", "Red")
data$HoursfromSLA <- as.numeric(data$HoursfromSLA)
ggplot(data, aes(x=data, fill = group)) +
geom_vline(xintercept = 0, colour="black") +
geom_histogram(mapping = aes(x=HoursfromSLA, y=..count../sum(..count..)*100), col=I("white"), show.legend=FALSE, bins=25) +
scale_fill_manual(values = c("Green" = "darkgreen", "Red" = "darkred")) +
scale_x_continuous(name = "Time to SLA", breaks = seq(-150, 720, 30)) +
scale_y_continuous(name = "[%]")
There might be a better way to do this with binned scales, but you could make a dataframe of the percentages for each column and work with that:
data$HoursfromSLA2 <- as.numeric(as.character(cut(data$HoursfromSLA, breaks=seq(-120,900,30),labels = seq(-120,900-30,30)+15)))
data2 <- aggregate(data=data, HoursfromSLA~HoursfromSLA2+group, length )
data2$perc <- 100*data2$HoursfromSLA/sum(data2$HoursfromSLA)
ggplot(data2, aes(x=HoursfromSLA2, y=perc)) +
geom_col(aes(fill=group),width =30) +
geom_text(aes(vjust=-.5,label=round(perc,1))) +
geom_line(aes(x=HoursfromSLA2-15,y=cumsum(perc))) +
geom_point(aes(x=HoursfromSLA2-15,y=cumsum(perc))) +
geom_text(vjust=-1,hjust=1,aes(x=HoursfromSLA2-15,y=cumsum(perc), label=round(cumsum(perc),1))) +
theme_bw()+ scale_fill_manual(values = c("Green" = "darkgreen", "Red" = "darkred")) +
scale_x_continuous(name = "Time to SLA", breaks = seq(-150, 720, 30)) +
scale_y_continuous(name = "[%]") +
geom_vline(xintercept=0) +
theme(legend.position = "none")
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 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:
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:
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)