ggplot(aes(x=MALE, y=AMOUNT, fill=MALE)) + geom_bar(stat="summary", fun="mean") +
ylab("Avg Amount") + theme(axis.title.x = element_blank())
How can I add the y value to the top of the bars given I've already created stat='summary' & fun='mean' when I created the graph?
To add the y value as label on top of your bars you can do:
geom_text(aes(label = after_stat(y)), stat = "summary", fun = "mean", vjust = -.1)
Using mtcars as example data and with some additional formatting of the label:
library(ggplot2)
ggplot(mtcars, aes(x = factor(cyl), y = mpg, fill = factor(cyl))) +
geom_bar(stat = "summary", fun = "mean") +
geom_text(aes(label = after_stat(sprintf("%.1f", y))), stat = "summary", fun = "mean", vjust = -.1) +
ylab("Avg Amount") +
theme(axis.title.x = element_blank())
For my rmarkdown document, I've put together two plots. The second plot is supposed to be a "zoomed in" version of the first plot.
---
title: TwoGraphs
output: pdf_document
---
```{r echo=FALSE,fig.show="hold", out.width="50%",fig.cap="Results"}
library(ggplot2)
data <- data.frame(
a_max = 1:10 * 10,
a_min = 1:10 * 9,
b_max = 1:10 * 5,
b_min = 1:10 * 4,
c_max = 1:10 * 0.002,
c_min = 1:10 * 0.001
)
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "A set"), fill = '#9B7FBF') +
geom_line(aes(y = a_max), color = "#1e152a", size=1.5, linetype="dashed") +
geom_line(aes(y = a_min), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "B set"), fill = '#ACD8DD') +
geom_line(aes(y = b_max), color = "#5ab1bb", size=1.5, linetype="dashed") +
geom_line(aes(y = b_min), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
# zoom in on c
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
```
Generating the plot works fine, however the legend I want to have is missing. Particularly I want the ribbon colors to be notated. The lines are not so important - I do mention in the caption though that dashed means most unsorted dataset, and a continuous line means that the dataset is sorted.
I have marked the legend I want to have in the screenshot below.
From this answer I have tried working adding scale_fill_manual, but it didn't show the legend. Using ggpubr as suggested here also didn't work for me, as demonstrated here.
---
title: TwoGraphs
output: pdf_document
---
```{r echo=FALSE,fig.show="hold", out.width="50%",fig.cap="Results"}
library(ggplot2)
data <- data.frame(
a_max = 1:10 * 10,
a_min = 1:10 * 9,
b_max = 1:10 * 5,
b_min = 1:10 * 4,
c_max = 1:10 * 0.002,
c_min = 1:10 * 0.001
)
p1 <- ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "Aha"), fill = '#9B7FBF') +
geom_line(aes(y = a_max), color = "#1e152a", size=1.5, linetype="dashed") +
geom_line(aes(y = a_min), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "Behe"), fill = '#ACD8DD') +
geom_line(aes(y = b_max), color = "#5ab1bb", size=1.5, linetype="dashed") +
geom_line(aes(y = b_min), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "Cehe"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
p2 <- ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "Cehe"), fill = '#deebd1') +
geom_line(aes(y = c_max), color = "#a5c882", size=1.5, linetype="dashed") +
geom_line(aes(y = c_min), color = "#a5c882", size=1.5) +
theme_bw()
library(ggpubr)
ggarrange(p1,p2,common.legend = TRUE,legend = "bottom")
```
How do I get the three filled areas to show up as a common legend? Is it also possible to add an overarching dashed-line / solid-line legend? (i.e., "dashed = unsorted, solid = sorted")
When you add a fill colour outside aes, it over-rides the one you put inside. You need to remove these and specify your colours inside scale_fill_manual. You can follow exactly the same process with the linetype aesthetic.
ggplot(data, aes(x = 1:10)) +
xlab("Time") + ylab("Amount") +
# a
geom_ribbon(aes(ymax = a_max, ymin = a_min, fill = "A set")) +
geom_line(aes(y = a_max, linetype = "sorted"), color = "#1e152a", size=1.5) +
geom_line(aes(y = a_min, linetype = "unsorted"), color = "#1e152a", size=1.5) +
# b
geom_ribbon(aes(ymax = b_max, ymin = b_min, fill = "B set")) +
geom_line(aes(y = b_max, linetype = "sorted"), color = "#5ab1bb", size=1.5) +
geom_line(aes(y = b_min, linetype = "unsorted"), color = "#5ab1bb", size=1.5) +
# c
geom_ribbon(aes(ymax = c_max, ymin = c_min, fill = "C set")) +
geom_line(aes(y = c_max, linetype = "sorted"), color = "#a5c882", size=1.5) +
geom_line(aes(y = c_min, linetype = "unsorted"), color = "#a5c882", size=1.5) +
theme_bw() +
scale_fill_manual(values = c(`A set` = '#9B7FBF', `B set` = '#ACD8DD',
`C set` = '#deebd1')) +
scale_linetype_manual(values = c(sorted = 1, unsorted = 2)) +
guides(linetype = guide_legend(override.aes = list(color = "black", size = 0.4))) +
labs(fill = "", linetype = "") +
theme(legend.position = "bottom",
legend.direction = "vertical")
With ggplot2 and GGally, I created this bar chart with proportions:
ggplot(mtcars, aes(x = factor(cyl), by = 1)) +
geom_bar(fill = "steelblue", stat = "prop") +
geom_text(aes(label = scales::percent(after_stat(prop), accuracy = 1)), stat = "prop", nudge_y = 0.5) +
theme_minimal() +
theme(aspect.ratio = 1.5)
However, on the y axis, I would like to change that to reflect the percentages on the bars. I would like to avoid hard coding the values like ylim = "40", but let it use the values in the chart.
Try this:
ggplot(mtcars, aes(x = cyl)) +
geom_bar(aes(y = ..prop..), fill = "steelblue", stat = "count") +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), stat= "count", vjust = -.5) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
Edit: if you want a factor on x axis try
ggplot(mtcars, aes(x = factor(cyl))) +
geom_bar(aes(y = (..count..)/sum(..count..)), fill = "steelblue", stat = "count") +
geom_text(aes(label = scales::percent(round((..count..)/sum(..count..), 2)),
y = ((..count..)/sum(..count..))), stat = "count", vjust = -.25) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
Edit2: with the GGally package you can use:
ggplot(mtcars, aes(x = factor(cyl), by = 1)) +
geom_bar(aes(y = ..prop..), fill = "steelblue", stat = "prop") +
geom_text(aes(label = scales::percent(..prop..), y = ..prop.. ), stat = "prop", vjust = -.5) +
ylim(0, 0.5) +
ylab("") +
theme_minimal() +
theme(aspect.ratio = 1.5)
I am trying to make the y axis of the second plot over 2 lines. Using '\n' for the first plot worked fine but using it on the second makes the text in odd places (maybe because of the italics).
p1 <- ggplot(data = new_data) +
geom_line(mapping = aes(x = Date,
y = Proportion,
group = Species,
colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",
Red_Snapper="firebrick2",
Sharksucker_Remora="darkolivegreen3",
Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\nM.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
grid.arrange(p1,p2)
You can try this:
geom_line(mapping = aes(x = Date,
y = Proportion,
group = Species,
colour = Species)) +
scale_colour_manual(values=c(Golden_Trevally="goldenrod2",
Red_Snapper="firebrick2",
Sharksucker_Remora="darkolivegreen3",
Juvenile_Remora="aquamarine2")) +
xlab("Date (2014-2018)") +
ylab("Total Presence \n Per Month ") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1)) +
theme(legend.position="top") +
labs(colour = "Hitchhiker Species")
new_data_counts <- new_data %>% select(Date, Count)
new_data_counts <- new_data_counts[!duplicated(new_data_counts),]
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
labs(x="Date (2014-2018)",
y=expression(atop(paste("Total Number of"), paste(italic("M.alfredi"), " Encounters")))) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
grid.arrange(p1,p2)
You need space between \n and M.alfredi in p2. Since there is no reproducible example, here is my suggestion for the second plot,
p2 <- ggplot(data = new_data_counts) +
geom_bar(mapping = aes(x = Date, y = Count), stat = 'identity') +
xlab("Date (2014-2018)") +
ylab("Total Number of "~italic(\n M.alfredi)~" Encounters") +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust = 1))
It should solve your problem.
I want to write the x-axis on all facets but without the scale.
The output
I tried to use scales='free_x' with scale_x_discrete(drop=FALSE) but it didn't work .
plot2 <- ggplot(counter2, aes(x = as.character(week) , y = freq,group=Delivery.time)) +
geom_point(colour = "red") + geom_line(colour = "red") +
labs(x = "Month", y = "Number Of Customers") +
facet_grid(.~Delivery.time) +
theme(axis.text.x = element_text(angle = 45, hjust=1)) +
geom_text(aes(label = freq),size = 2.5, hjust = .5, vjust=-0.5) +
facet_wrap(~Delivery.time, ncol = 1, scales = x) +
scale_x_discrete(drop=FALSE)
Plot with scaled x-axis
I want all a-axis