How to add count labels in ggplot2 barplot? - r

I'm trying to place count labels in a ggplot2 barplot and I haven't been able to do it. I need to display the number of pixels within each temperature range.
The dataframe was built from a raster: EneroT5cmSC
datene <- as.data.frame(EneroT5cmSC,xy=TRUE)%>%drop_na()
datene$cuts <- cut(datene$layer, breaks=seq(21, 29, length.out=12))
dput:
datene_stuc <- structure(
list(
x = c(-57.063098328,-57.021448328,-56.996458328,-56.988128328),
y = c(-30.087481664,-30.087481664,-30.087481664,-30.087481664),
layer = c(
25.6227328470624,
26.6386584334308,
26.0636709134397,
26.0580615984563
),
cuts = structure(
c(7L, 9L,
8L, 8L),
.Label = c(
"(20,20.8]",
"(20.8,21.6]",
"(21.6,22.5]",
"(22.5,23.3]",
"(23.3,24.1]",
"(24.1,24.9]",
"(24.9,25.7]",
"(25.7,26.5]",
"(26.5,27.4]",
"(27.4,28.2]",
"(28.2,29]"
),
class = "factor"
)
),
row.names = c(NA,
4L),
class = "data.frame")
Barplot code:
ggplot() +
geom_bar(data = datene, aes(cuts, fill = cuts)) +
scale_fill_viridis_d(option = "B",'Temp (Cº)') +
theme(axis.title.x=element_blank(), axis.title.y=element_blank()) +
geom_text(aes(label = ..count..), stat = "count", vjust = 1.5, colour = "black")

If you replace the code arguments from geom_bar to ggplot() and change vjust = -1 it works like this:
datene_stuc <- structure(
list(
x = c(-57.063098328,-57.021448328,-56.996458328,-56.988128328),
y = c(-30.087481664,-30.087481664,-30.087481664,-30.087481664),
layer = c(
25.6227328470624,
26.6386584334308,
26.0636709134397,
26.0580615984563
),
cuts = structure(
c(7L, 9L,
8L, 8L),
.Label = c(
"(20,20.8]",
"(20.8,21.6]",
"(21.6,22.5]",
"(22.5,23.3]",
"(23.3,24.1]",
"(24.1,24.9]",
"(24.9,25.7]",
"(25.7,26.5]",
"(26.5,27.4]",
"(27.4,28.2]",
"(28.2,29]"
),
class = "factor"
)
),
row.names = c(NA,
4L),
class = "data.frame")
library(ggplot2)
ggplot(data = datene_stuc, aes(cuts, fill = cuts)) +
geom_bar() +
geom_text(aes(label = ..count..), stat = "count", vjust = -1, colour = "black") +
scale_fill_viridis_d(option = "B",'Temp (Cº)') +
theme(axis.title.x=element_blank(), axis.title.y=element_blank())
Created on 2022-07-17 by the reprex package (v2.0.1)

Related

ggplot with arrows to connect different variables

i have three variables in my dataset:
school (School)
actual score (actual_score)
expected score (expected_score)
and need to do this graph
So far I have
data%>%
mutate(School=fct_reorder(School, actual_score)
)%>%
ggplot(aes(x=School))+
geom_point(aes(y=actual_score), colour="red")+
geom_point(aes(y= expected_score), colour="blue")
But they are just points... how to connect them?
structure(list(School = structure(c(9L,
6L, 8L, 2L, 1L), levels = c("11278", "11274", "11285", "11289",
"11280", "01424", "11290", "11272", "01206", "11286"), class = "factor"),
actual_score = c(453.4875, 423.375757575758, 441.481481481482,
375.103846153846, 363.621428571429), expected_score = c(452.489150512886,
428.002515274828, 439.209772701724, 384.917346549729, 382.216349569884
)), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -5L), .rows = structure(list(
1:5), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))
Your dput result is slightly corrupt, so I slightly modified it.
You can use geom_linerange to connect the points.
I also included the rest of the graph as placing the labels is a bit tricky.
library(tidyverse)
data <- tibble(
School = structure(
c(9L, 6L, 8L, 2L, 1L),
levels = c("11278", "11274", "11285", "11289", "11280", "01424", "11290", "11272", "01206", "11286"),
class = "factor"),
actual_score = c(453.4875, 423.375757575758, 441.481481481482, 375.103846153846, 363.621428571429),
expected_score = c(452.489150512886, 428.002515274828, 439.209772701724, 384.917346549729, 382.216349569884))
data%>%
mutate(School = fct_reorder(fct_relabel(School, ~ paste("School", LETTERS[1:(length(.))])), actual_score)) %>%
ggplot(aes(x = School)) +
geom_linerange(aes(ymin = actual_score, ymax = expected_score)) +
geom_point(aes(y = actual_score, color = "Actual", shape = "Acutal"), size = 3) +
geom_text(aes(y = actual_score - 5 + 10 * (actual_score > expected_score), label = round(actual_score))) +
geom_point(aes(y = expected_score, color = "Expected", shape = "Expected"), size = 3) +
geom_text(aes(y = expected_score - 5 + 10 * (actual_score < expected_score), label = round(expected_score))) +
scale_color_manual(name = NULL,
labels = c("Acutal", "Expected"),
values = c("blue", "red")) +
scale_shape_manual(name = NULL,
labels = c("Acutal", "Expected"),
values = c(16, 17)) +
labs(y = "Average NAPLAN score", x = NULL) +
theme_minimal() +
theme(legend.position = "bottom",
panel.grid.major.x = element_blank())
Created on 2022-12-19 with reprex v2.0.2
To connect your points you could use a geom_segment. And to get the different shapes map on the shape aesthetic. Also do the same for color to get a legend reflecting both shape and color. The rest is some styling plus some additional geom_text layers for the labels.
library(dplyr)
library(ggplot2)
library(forcats)
data %>%
mutate(School = fct_reorder(School, actual_score)) %>%
ggplot(aes(x = School)) +
geom_segment(aes(xend = School, y = actual_score, yend = expected_score),
colour = "grey80", linewidth = 1
) +
geom_point(aes(y = actual_score, colour = "Actual", shape = "Actual"), size = 3) +
geom_point(aes(y = expected_score, colour = "Expected", shape = "Expected"), size = 3) +
geom_label(aes(
y = actual_score, label = round(actual_score),
vjust = ifelse(actual_score > expected_score, 0, 1)
), label.size = NA, label.padding = unit(10, "pt"), fill = NA) +
geom_label(aes(
y = expected_score, label = round(expected_score),
vjust = ifelse(expected_score > actual_score, 0, 1)
), label.size = NA, label.padding = unit(10, "pt"), fill = NA) +
scale_color_manual(values = c("red", "blue")) +
scale_shape_manual(values = c(16, 17)) +
scale_y_continuous(breaks = seq(320, 480, 40), limits = c(320, 480)) +
labs(color = NULL, shape = NULL, x = NULL, y = "Average NAPLAN Score") +
theme_minimal() +
theme(
legend.position = "bottom",
axis.title.y = element_text(face = "bold"),
panel.grid.major.x = element_blank(),
panel.grid.minor = element_blank()
)
DATA
data <- structure(list(
School = structure(c(9L, 6L, 8L, 2L, 1L), levels = c(
"11278", "11274", "11285", "11289",
"11280", "01424", "11290", "11272", "01206", "11286"
), class = "factor"),
actual_score = c(
453.4875, 423.375757575758, 441.481481481482,
375.103846153846, 363.621428571429
), expected_score = c(
452.489150512886,
428.002515274828, 439.209772701724, 384.917346549729, 382.216349569884
)
), class = c("tbl_df", "tbl", "data.frame"), row.names = c(NA, -5L), .rows = structure(list(1:5), ptype = integer(0), class = c(
"vctrs_list_of",
"vctrs_vctr", "list"
)))

Adding p value on top of grouped bar plot

This is my data which I'm trying to plot
dput(results)
structure(list(ontology = c("CC", "BP", "MF", "CC", "BP", "MF",
"CC", "BP", "MF"), breadth = structure(c(3L, 3L, 3L, 2L, 2L,
2L, 1L, 1L, 1L), .Label = c("10", "30", "100"), class = "factor"),
enrichment = c(4.09685904270847, 8.04193317540539, 5.5801230522415,
4.52127958016442, 8.9221766387218, 5.68189764335457, 4.25046722366786,
9.49038239297713, 6.75423163834793), p = c(0, 0, 0, 0, 0,
0, 2.09057402562873e-221, 0, 0)), class = "data.frame", row.names = c(NA,
-9L))
My code
results = read.delim("data/GO/LC-GO-enrichment_new.txt") %>%
mutate(breadth = factor(breadth))
p = ggplot(results, aes(x = breadth, y = enrichment, fill = ontology,
color = ontology)) +
geom_col(position = 'dodge', width = 0.8) +
labs(x = "Breadth", y = "Odds ratio") +
scale_fill_manual(values = ryb8[c(1, 5, 8)], name = "Ontology") +
scale_color_manual(values = darken(ryb8[c(1, 5, 8)], 1.3),
name = "Ontology") +
scale_y_log10(expand = c(0.01, 0)) +
sci_theme
p
I get something like this
is there a way the pvalue can be added similar to this
or its done post making the figure manually .
Any help or suggestion would be really helpfu;
You could simply add the p values as a text layer. Note though, that in your data, each bar has a p value, so it's not clear where the groupwise p values are coming from.
library(ggplot2)
ggplot(results, aes(x = breadth, y = enrichment, fill = ontology)) +
geom_col(position = 'dodge', width = 0.8,
aes(color = after_scale(colorspace::darken(fill, 1.3)))) +
geom_text(aes(label = paste("p", scales::pvalue(p)), group = ontology),
vjust = -1, position = position_dodge(width = 0.8)) +
labs(x = "Breadth", y = "Odds ratio", fill = "Ontology") +
scale_fill_manual(values = c("#d63228", "#dff2f8", "#4575b5")) +
scale_y_log10(expand = c(0.05, 0)) +
theme_classic(base_size = 16) +
theme(legend.position = "top")

Displaying labels on x-axis with "yearmon" variable (ggplot)

I am trying to display all month values on my x-axis, which is formatted as a "yearmon" variable
My data are structured as follows:
Print data example
dput(collective_action_monthly[1:4, ])
ouptut:
structure(list(collective_action = structure(c(2L, 2L, 2L, 2L
), .Label = c("0", "1"), class = "factor"), treatment_details = c("pre",
"pre", "pre", "pre"), month_year = structure(c(2011.41666666667,
2011.75, 2011.83333333333, 2011.91666666667), class = "yearmon"),
n = c(22L, 55L, 15L, 207L), collective_action_percentage = c(0.0124223602484472,
0.031055900621118, 0.00846979107848673, 0.116883116883117
), am = structure(c(2L, 2L, 2L, 2L), .Label = c("post", "pre"
), class = "factor")), class = c("grouped_df", "tbl_df",
"tbl", "data.frame"), row.names = c(NA, -4L), groups = structure(list(
treatment_details = "pre", .rows = structure(list(1:4), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), class = c("tbl_df", "tbl", "data.frame"
), row.names = c(NA, -1L), .drop = TRUE))
This my code to visualize the trend using bar graphs by month:
ggplot(data = collective_action_monthly, aes(x = month_year, y = collective_action_percentage)) +
geom_bar(stat = "identity", position=position_dodge()) +
scale_fill_grey() +
ylab("percentage") +
theme(text=element_text(size=10)) +
theme(plot.title = element_text(size = 10, face = "bold")) +
scale_y_continuous(labels = percent_format(accuracy = 1)) +
theme(axis.text.x = element_text(angle = 90, vjust = 0.5)) +
theme_bw()
which produces:
However, rather than only showing three months in the x-axis, I would like to show all months. I also tried adding "scale_x_continuous(labels = 0:14, breaks = 0:14) " to the code above, but it still does not display months:
Ideally, I would like to produce a graph as the one below, but with months instead of years.
The zoo packages includes scale_x_yearmon, so you can do:
library(zoo)
library(ggplot2)
ggplot(data = collective_action_monthly,
aes(x = month_year, y = collective_action_percentage)) +
geom_col(position = position_dodge(preserve = "single")) +
scale_y_continuous(labels = scales::percent_format(accuracy = 1),
name = "percentage") +
scale_x_yearmon(breaks = seq(2011.25, 2012, 1/12),
limits = c(2011.25, 2012)) +
theme_bw(base_size = 10) +
theme(plot.title = element_text(size = 10, face = "bold"),
axis.text.x = element_text(angle = 90, vjust = 0.5))
ggplot doesn't have a yearmon scale built in--looks like the zoo package does, but it doesn't have a convenient way to specify "breaks every month"--so I would suggest converting to Date class and using scale_x_date. I've deleted most of your theme stuff to make the changes I've made more obvious (the theming didn't seem relevant to the issue).
ggplot(data = collective_action_monthly, aes(x = as.Date(month_year), y = collective_action_percentage)) +
geom_bar(stat = "identity", position=position_dodge()) +
scale_fill_grey() +
scale_x_date(date_breaks = "1 month", date_labels = "%b %Y") +
ylab("percentage") +
theme_bw()

R how to prevent ggplot geom_text() from using new database data on a named plot object

I am attempting to make a series of plots using the same code with unique coral species databases.
Databases
data_1 <- structure(list(Site_long = structure(c(1L, 1L, 2L, 2L), .Label = c("Hanauma Bay",
"Waikiki"), class = "factor"), Shelter = structure(c(1L, 2L,
1L, 2L), .Label = c("Low", "High"), class = c("ordered", "factor"
)), mean = c(1.19986885018767, 2.15593884020962, 0.369605100791602,
0.31005865611133), sd = c(2.5618758944073, 3.67786619671933,
1.0285671157698, 0.674643037178562), lower = c(0.631321215232725,
1.33972360808602, 0.141339007832154, 0.160337623931733), upper = c(1.76841648514261,
2.97215407233321, 0.59787119375105, 0.459779688290928), sample_size = c(78L,
78L, 78L, 78L)), row.names = c(NA, -4L), groups = structure(list(
Site_long = structure(1:2, .Label = c("Hanauma Bay", "Waikiki"
), class = "factor"), .rows = structure(list(1:2, 3:4), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
data_2 <- structure(list(Site_long = structure(c(2L, 2L, 1L, 1L), .Label = c("Hanauma Bay",
"Waikiki"), class = "factor"), Shelter = structure(c(1L, 2L,
1L, 2L), .Label = c("Low", "High"), class = c("ordered", "factor"
)), mean = c(0.695203162997812, 0.838720069947102, 0.76957780057238,
0.771070502382599), sd = c(1.17117437618039, 1.02766824928792,
1.43499288333539, 1.28634022958585), lower = c(0.435288768568787,
0.610653459098997, 0.451115141323908, 0.485597776371556), upper = c(0.955117557426838,
1.06678668079521, 1.08804045982085, 1.05654322839364), sample_size = c(78L,
78L, 78L, 78L)), row.names = c(NA, -4L), groups = structure(list(
Site_long = structure(1:2, .Label = c("Hanauma Bay", "Waikiki"
), class = "factor"), .rows = structure(list(3:4, 1:2), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
When I run my code on the first species database (data_1), the barplots and associated error bar annotations render correctly. Notice I also made a new variable "data" that will be the same object used in later for species 2. In order to keep this plot to make a composite of a number of plots later, I named the plot "species_1_plot" to save it to the global environment.
Code for Species 1 Plot
data <- data_1
mult_compare_recruitment <- c("A", "A", "A", "A")
data <- data[c(3, 4, 1, 2),]
data$Shelter <- factor(data$Shelter, levels = c("Low", "High"))
# reorder summary dataframe for plotting
position <- c("Waikiki", "Hanauma Bay")
# ggplot2 barplot position with Waikiki (Low-High Shelter) and Hanauma Bay
recruitment_plot_3 <- ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
scale_x_discrete(limits = position) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare_recruitment, y = data$upper), vjust = -.5, position = position_dodge(width = 0.8), size = 4) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = expression(paste("Coral recruitment per m"^"2"))) +
theme_classic(base_size = 14.5) +
theme(text = element_text(size = 18), axis.title.x = element_blank(),
legend.position = "none", axis.text.y = element_text(angle = 90))
species_1_plot <- recruitment_plot_3
species_1_plot
In order to create my next plot, I run the same code on a different species database (data_2) while once again assigning the new database to the object "data". Once again, I saved the new plot "species_2_plot" to the global environment.
Code for Species 2 Plot
data <- data_2
mult_compare_recruitment <- c("A", "A", "B", "B")
data <- data[c(3, 4, 1, 2),]
data$Shelter <- factor(data$Shelter, levels = c("Low", "High"))
# reorder summary dataframe for plotting
position <- c("Waikiki", "Hanauma Bay")
# ggplot2 barplot position with Waikiki (Low-High Shelter) and Hanauma Bay
recruitment_plot_3 <- ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
scale_x_discrete(limits = position) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare_recruitment, y = data$upper), vjust = -.5, position = position_dodge(width = 0.8), size = 4) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = expression(paste("Coral recruitment per m"^"2"))) +
theme_classic(base_size = 14.5) +
theme(text = element_text(size = 18), axis.title.x = element_blank(),
legend.position = "none", axis.text.y = element_text(angle = 90))
species_2_plot <- recruitment_plot_3
species_2_plot
The problem is, when I plot the first species plot again (species_1_plot), the data are correct (bars), but the height of text annotations and their letter values are not correct. They are in fact the values from species_2_plot.
species_1_plot
I saved each plot to the global environment with a unique name knowing this would be an issue. But despite this, geom_text() seems to be using data from the second plot (code that is in the global environment) instead despite that the actual data (bars) in the plot are correct (from species_plot_1). My understanding was that when you name a plot as an object (species_1_plot and species_2_plot) that its akin to saving the plot and therefore preventing any changes to plot and annotations unless specified. Is there any way to prevent this from happening without specifically naming the databases (data_1 and data_2)? All input is appreciated. Thanks in advance!
I would suggest you to use an approach with a function. The fact of using data twice is maybe changing the environment and as a result the plots change. I have made a function with parameters for data, position and recruitment and I display the outputs. You have to fill them in the same way you defined that variables in your code. Functions work on internal environments so there might not be issues about how data is processed. Here the code where I used the data you shared:
library(ggplot2)
#Function
myplotfunc <- function(x,y,z)
{
data <- x
mult_compare_recruitment <- y
data <- data[c(3, 4, 1, 2),]
data$Shelter <- factor(data$Shelter, levels = c("Low", "High"))
# reorder summary dataframe for plotting
position <- z
# ggplot2 barplot position with Waikiki (Low-High Shelter) and Hanauma Bay
plot <- ggplot(data = data, aes(fill=Shelter, y=mean, x=Site_long)) +
geom_bar(position = "dodge", stat="identity", width = .8) +
scale_x_discrete(limits = position) +
geom_errorbar(aes(ymin = lower, ymax = upper), position = position_dodge(.8), width = .1) +
geom_text(aes(label = mult_compare_recruitment, y = data$upper), vjust = -.5, position = position_dodge(width = 0.8), size = 4) +
scale_fill_grey(name = "Shelter", start = .8, end = .2) +
labs(x = "Site", y = expression(paste("Coral recruitment per m"^"2"))) +
theme_classic(base_size = 14.5) +
theme(text = element_text(size = 18), axis.title.x = element_blank(),
legend.position = "none", axis.text.y = element_text(angle = 90))
return(plot)
}
#Code
o1 <- myplotfunc(x=data_1,y=c("A", "A", "A", "A"),z=c("Waikiki", "Hanauma Bay"))
o2 <- myplotfunc(x=data_2,y=c("A", "A", "B", "B"),z=c("Waikiki", "Hanauma Bay"))
Outputs:

adjust width of dodge bar chart with ggplot when other series is completely missing

I have prepared below function to plot dodge chart using ggplot:
frq_dodge2 <- function(chart_data) {
sapphire<-c("#00A8C8","#006D9E","#002C77","#A6E2EF","#51d5ee","#1d5cc7")
g<-ggplot(chart_data, aes(x=X, y=value,fill=Q))
chart <- g+
geom_bar(position = position_dodge2(preserve = "single",width=0.9),stat='identity') +
scale_fill_manual(values = sapphire)+
labs(x= NULL, y= NULL, subtitle=NULL) +
ylab(NULL) +
geom_text(chart_data = subset(chart_data,value!=0),aes(label=paste0(value,"%")),
position=position_dodge2(width=0.9), vjust=-0.25,
size=3,fontface="bold", colour="#404040") +
labs(x=NULL, y=NULL)+
scale_y_continuous( labels = number_format(suffix = "%"),
limits = c(min(0,min(chart_data$value)+min(chart_data$value)),
max(0,max(chart_data$value) + max(chart_data$value) / 10)))+
scale_x_discrete(labels = function(x) str_wrap(x, width = 10),limits=unique(chart_data$Stats))
chart
}
The issue when in the data one of the series is completely missing the bars are too wide, not looking good. For example for the below data the bars are plotted too wide.
> dput(expat)
structure(list(X = structure(c(1L, 1L), .Label = c("Less than 50",
"50-100", "100-250", "250-500", "500-1000", "1000-3000", "3000-5000",
"more than 5000"), class = "factor"), Q = structure(1:2, .Label = c("2018 (Actual)",
"2019 (Forecast)"), class = "factor"), value = c(100, 100)), class = "data.frame", row.names = c(NA,
-2L))
frq_dodge2(expat) will give the graph output
whereas in other data where the other series is not completely missing plot is ok:
> dput(localplus)
structure(list(X = structure(c(6L, 1L, 6L, 2L, 1L), .Label = c("Less than 50",
"50-100", "100-250", "250-500", "500-1000", "1000-3000", "3000-5000",
"more than 5000"), class = "factor"), Q = structure(c(1L, 1L,
2L, 2L, 2L), .Label = c("2018 (Actual)", "2019 (Forecast)"), class = "factor"),
value = c(14, 86, 11, 22, 67)), class = "data.frame", row.names = c(NA,
-5L))
I had used preserve="single" to fix the bars width in case of missing data in other series but this is not helping if other series is completely missing in the data (like in expat).
Is there any way to fix this?
As mentioned in this answer, you need to have drop = FALSE set in your x scale calls.
In your function, that's the last line :
scale_x_discrete(labels = function(x) str_wrap(x, width = 10), drop = F)
For me, this yields the following :
EDIT : remove unneeded labels in x axis
Just check which levels are missing and change their label to "". The full function thus becomes :
frq_dodge2 <- function(chart_data) {
sapphire<-c("#00A8C8","#006D9E","#002C77","#A6E2EF","#51d5ee","#1d5cc7")
g<-ggplot(chart_data, aes(x=X, y=value,fill=Q))
lvs <- levels(chart_data$X)
miss_lvs <- which(!lvs%in%unique(chart_data$X))
lvl_labs <- lvs
lvl_labs[miss_lvs] <- ""
chart <- g+
geom_bar(position = position_dodge2(preserve = "single",width=0.9),stat='identity') +
scale_fill_manual(values = sapphire)+
labs(x= NULL, y= NULL, subtitle=NULL) +
ylab(NULL) +
geom_text(data = subset(chart_data,value!=0),aes(label=paste0(value,"%")),
position=position_dodge2(width=0.9), vjust=-0.25,
size=3,fontface="bold", colour="#404040") +
labs(x=NULL, y=NULL)+
scale_y_continuous( labels = number_format(suffix = "%"),
limits = c(min(0,min(chart_data$value)+min(chart_data$value)),
max(0,max(chart_data$value) + max(chart_data$value) / 10)))+
scale_x_discrete(labels = lvl_labs, drop = F)
chart
}
and yields
As a side note, you had written chart_data = subset... in the geom_text, instead of data = subset....

Resources