This question already has an answer here:
Position geom_text on dodged barplot
(1 answer)
Closed 4 years ago.
When adding the percentages to the barplots (see code below), the three percentages that belong to the same "variable" are on the same height, and thus not aligned to their respective bar. How to change that?
#Sum plot
myd<- data.frame( var1 = rep(c("Newly infected","Mortality","TDR level"),each=3),
samp = rep(c("Scen1","Scen2","Scen3"),3),
V3 = c(3.5,2,NA,8,2,NA,4,5,NA)/1.5, V2 = c(3.5,2,NA,8,3,NA,4,4.3,NA), V1 = c(1.5,0.2,5,5,3,0.2,4,-5,2) )
# rshaping data to long form for ggplot2
library(reshape2)
library(ggplot2)
meltd<- melt(myd, id.vars=1:2)
ggplot(meltd, aes(x=var1, y=value, fill=variable)) +
geom_bar(stat="identity",position=position_dodge(width=0.6),width=0.5) +
facet_grid(samp~., switch = "y", scales = "free_y", space = "free") +
theme_bw()+
theme(legend.position = "bottom")+
theme(strip.placement = "outside")+
theme(axis.title.x = element_blank()) +
theme(axis.title.y = element_blank()) +
theme(axis.text.y = element_text(colour="black"))+
theme(strip.text.y = element_text(size = 12, colour = "black"))+
#scale_y_continuous(labels=percent,limits = c(0,1))
coord_flip()+
scale_fill_manual("legend", values = c("V3"="orange","V2" = "red", "V1" = "blue", "Baseline" = "black"))+
geom_text(data=meltd, aes(label=paste0(round(value*100,1),"%"), y=value+0.4*sign(value)), size=4)
Do you mean this?
ggplot(meltd, aes(x = var1, y = value, fill = variable, label = paste0(round(value * 100, 1), "%"))) +
geom_bar(stat = "identity", position = position_dodge(width = 0.6), width = 0.5) +
facet_grid(samp ~ ., switch = "y", scales = "free_y", space = "free") +
coord_flip() +
scale_fill_manual(
"legend",
values = c("V3" = "orange", "V2" = "red", "V1" = "blue", "Baseline" = "black")) +
geom_text(aes(y = value + 0.4 * sign(value)), position = position_dodge(width = 0.6)) +
theme_bw() +
theme(
legend.position = "bottom",
strip.placement = "outside",
axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(colour="black"),
strip.text.y = element_text(size = 12, colour = "black"))
Use position = position_dodge(width = 0.6) inside geom_text to dodge labels (equivalent to dodging the bars in geom_bar).
Related
I have a genome-wide dataset that I'm trying to plot in the following way:
Have each chromosome be a separate color
Have specific windows highlighted by a bar (I'm using geom_vline) - this I'm getting from a
separate table
Have only geom_vline feature in the legend
I have tried many different things, but it seems I cannot have all three together!
Here is the link to both datasets:
allStats & allStats_fstPi_group15
With this code, I can have the first 2, but not the 3rd:
ggplot(allStats, aes(x = mid2, y = Fst_group1_group5,
color = as_factor(scaffold))) +
geom_point(size = 2) +
geom_vline(xintercept = chrom$add, color = "grey") +
scale_y_continuous(expand = c(0,0), limits = c(0, 1)) +
scale_x_continuous(labels = chrom$chrID, breaks = axis_set$center) +
scale_color_manual(values = rep(c("#276FBF", "#183059"), unique(length(chrom$chrID)))) +
scale_size_continuous(range = c(0.5,3)) +
labs(x = NULL,
y = "Fst SBM vs OC") +
theme_minimal() +
theme(
legend.position = "none",
panel.grid.major.x = element_blank(),
panel.grid.minor.x = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
axis.title.y = element_text(),
axis.text.x = element_text()) +
geom_vline(data = allStats_fstPi_group15,
aes(xintercept = allStats_fstPi_group15$mid2),
color = "orange", show.legend = T)
With this one I can get 2 and 3 only (I'm not able to color code each block separately):
cols <- c("SBM vs OC" = rep(c("#276FBF", "#183059"), unique(length(chrom$chrID))),
"90th percentile (Fst vs Pi)" = "orange")
ggplot(allStats, aes(x = mid2)) +
geom_point(aes(y = Fst_group1_group5,
color = as_factor(scaffold)),
size = 2) +
geom_vline(data = allStats_fstPi_group15,
aes(xintercept = allStats_fstPi_group15$mid2,
color = "90th percentile (Fst vs Pi)")) +
scale_color_manual(values = cols)
I've seen the issue with the legend being that color needs to be within aes(), so my question is: is it impossible what I'm trying to do?
Below is a simple bubble plot for three character traits (Lg_chr, Mid_chr, and Sm_chr) across three locations.
All good, except that because the range of Lg_chr is several orders of magnitude larger than the ranges for the other two traits, it swamps out the area differences between the smaller states, making the differences very difficult to see - for example, the area of the points for for Location_3's Mid_chr (70) and Sm_chr (5), look almost the same.
Is there a way to set a conditional size scale based on name in ggplot2 without having to facit wrap them? Maybe a conditional statement for scale_size_continuous(range = c(<>, <>)) separately for Lg_chr, Mid_chr, and Sm_chr?
test_df = data.frame(lg_chr = c(100000, 150000, 190000),
mid_chr = c(50, 90, 70),
sm_chr = c(15, 10, 5),
names = c("location_1", "location_2", "location_3"))
#reformat for graphing
test_df_long<- test_df %>% pivot_longer(!names,
names_to = c("category"),
values_to = "value")
#plot
ggplot(test_df_long,
aes(x = str_to_title(category),
y = str_to_title(names),
colour = str_to_title(names),
size = value)) +
geom_point() +
geom_text(aes(label = value),
colour = "white",
size = 3) +
scale_x_discrete(position = "top") +
scale_size_continuous(range = c(10, 50)) +
scale_color_manual(values = c("blue", "red",
"orange")) +
labs(x = NULL, y = NULL) +
theme(legend.position = "none",
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank()) ```
Edit:
You could use ggplot_build to manually modify the point layer [[1]] to specify the sizes of your points like this:
#plot
p <- ggplot(test_df_long,
aes(x = str_to_title(category),
y = str_to_title(names),
colour = str_to_title(names),
size = value)) +
geom_point() +
geom_text(aes(label = value),
colour = "white",
size = 3) +
scale_x_discrete(position = "top") +
scale_color_manual(values = c("blue", "red",
"orange")) +
labs(x = NULL, y = NULL) +
theme(legend.position = "none",
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank())
q <- ggplot_build(p)
q$data[[1]]$size <- c(7,4,1,8,5,2,9,6,3)*5
q <- ggplot_gtable(q)
plot(q)
Output:
You could use scale_size with a log10 scale to make the difference more visuable like this:
#plot
ggplot(test_df_long,
aes(x = str_to_title(category),
y = str_to_title(names),
colour = str_to_title(names),
size = value)) +
geom_point() +
geom_text(aes(label = value),
colour = "white",
size = 3) +
scale_size(trans="log10", range = c(10, 50)) +
scale_x_discrete(position = "top") +
scale_color_manual(values = c("blue", "red",
"orange")) +
labs(x = NULL, y = NULL) +
theme(legend.position = "none",
panel.background = element_blank(),
panel.grid = element_blank(),
axis.ticks = element_blank())
Output:
I want to lower the height of the labels in my dodged bar chart so that they are not on the edge of the bar.
I tried using height= in position_dodge() however this gives the error 'unused argument' since height is not an argument in position_dodge(). This is my code:
newdata <- read.table(text = "2010 2018
healthcare 0.09940358 0.09256198
allsectors 0.14713699 0.15306588", sep = " ", header = TRUE)
library(reshape)
datnew <- melt(cbind(newdata, ind = rownames(newdata)), id.vars = c('ind'))
library(ggplot2)
library(scales)
library(dplyr)
ggplot(datnew, aes(x = variable, y = value, fill = ind)) +
geom_col(position = "dodge", colour = 'black') +
geom_text(size = 3.5, position = position_dodge(width=1), aes(label =
scales::percent(value))) +
scale_x_discrete(labels = c('2010', '2018')) +
scale_y_continuous(labels = percent_format()) +
scale_fill_manual(name = "sector" , values = c("brown3", "darkgrey")) +
xlab('Year') +
ylab('% of workforce') +
ggtitle('figure 4', subtitle = 'Workers under the age of 25')
theme_bw() +
theme(plot.title=element_text(lineheight = NULL, hjust=0.5, face="bold",
color="black", size=16), plot.subtitle = element_text(lineheight = NULL,
hjust=0.5, color = 'black', size=14), panel.border = element_blank(),
panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black")) + expand_limits(y = 0)
As explained, I want to lower the position of the labels in the bars.
How to modify the "New infected", "Mortality" and "TDR level" text characteristics? I want to make it bigger and in bold. How to modify the "Scen1", "Scen2" and "Scen3" text?
I already looked into the R documentation but didn't find an answer in the particular case where barplot are used with "dodge".
Here is the code:
myd<- data.frame( var1 = rep(c("Newly infected","Mortality","TDR level"),each=3),
samp = rep(c("Scen1","Scen2","Scen3"),3),
V3 = c(3.5,2,NA,8,2,NA,4,5,NA)/1.5, V2 = c(3.5,2,NA,8,3,NA,4,4.3,NA), V1 = c(1.5,0.2,5,5,3,0.2,4,-5,2) )
# rshaping data to long form for ggplot2
library(reshape2)
meltd<- melt(myd, id.vars=1:2)
#meltd<- meltd[-which(is.na(meltd$value)),]
ggplot(meltd, aes(x = var1, y = value, fill = variable, label = paste0(round(value * 100, 1), "%"))) +
geom_bar(stat = "identity", position = position_dodge(width = 0.6), width = 0.5) +
facet_grid(samp ~ ., switch = "y", scales = "free_y", space = "free") +
coord_flip() +
scale_fill_manual("legend",values = c("V3" = "orange", "V2" = "red", "V1" = "blue", "Baseline" = "black")) +
geom_text(aes(y = value + 0.4 * sign(value)), position = position_dodge(width = 0.6)) +
theme_bw() +
theme(
legend.position = "none",
strip.placement = "outside",
#axis.title.x = element_blank(),
axis.title.y = element_blank(),
axis.text.y = element_text(colour="black"),
strip.text.y = element_text(size = 12, colour = "black"))+
ylab("Relative change (in %)")
Here is the plot:
You're pretty much there, I just added face, and size arguments:
axis.text.y = element_text(colour="black", face='bold',size=12),
strip.text.y = element_text(size = 12, colour = "black", face='bold'))+
ylab("Relative change (in %)")
Or to change the size of the value labels add size to geom_text:
geom_text(aes(y = value + 0.4 * sign(value)),
position = position_dodge(width = 0.6), size=6)
Full code:
ggplot(meltd, aes(x = var1, y = value, fill = variable, label = paste0(round(value * 100, 1), "%"))) +
geom_bar(stat = "identity", position = position_dodge(width = 0.6), width = 0.5) +
facet_grid(samp ~ ., switch = "y", scales = "free_y", space = "free") +
coord_flip() +
scale_fill_manual("legend",values = c("V3" = "orange", "V2" = "red", "V1" = "blue", "Baseline" = "black")) +
geom_text(aes(y = value + 0.4 * sign(value)), position = position_dodge(width = 0.6), size=6)+
theme_bw() +
theme(
legend.position = "none",
strip.placement = "outside",
#axis.title.x = element_blank(),
axis.title.y = element_blank(),
# added face and size arguments
axis.text.y = element_text(colour="black", face='bold',size=12),
strip.text.y = element_text(size = 12, colour = "black", face='bold'))+
ylab("Relative change (in %)")
I would like to know if it's possible to modify the ticks of x axis with a ggplot pie chart.
Here what I can do :
# Some colors
couleurs <- data.frame(
id=seq(1,17,1),
mix=c(c(rep(1,6),rep(2,7),rep(3,4))),
html=c("#A00020","#109618","#388EE4","#C484D1","#FFAA33","#CCCDD0","#004AC5","#F80094","#CB5023","#638995","#33CFCF","#95DC4E","#F7D633","#5C403C","#F72020","#00D96C","#FDE4C5")
)
couleurs$html <- factor(couleurs$html, levels = couleurs$html[order(couleurs$id, decreasing = FALSE)])
# Data
activite <- data.frame(label=c("B to B","B to C","B to B / B to C", "B to B"), cible=c(rep("Externe",3), "Interne"), nb=c(12,9,3,12))
activite$label <- factor(activite$label, levels = activite$label[order(activite$nb[activite$cible=="Externe"], decreasing = TRUE)])
library(plyr)
activite<-ddply(activite,.(cible),transform,pc=(nb/sum(nb))*100)
activite
# Pie chart
library(ggplot2)
ggplot(data = activite, aes(x = "", y = nb, fill = label )) +
geom_bar(stat = "identity", position = position_fill(), width = 1) +
coord_polar(theta= "y", start = 0, direction = -1) +
labs(fill="") +
scale_fill_manual(values=as.character(couleurs$html[1:nrow(activite)]), labels=paste(activite$label,"\t\t\t",sep="")) +
geom_text(aes(label = paste(pc,"%", sep=" ")), size=4, colour = "white", fontface = "bold", position = position_fill(vjust = 0.5)) +
theme(strip.text = element_text(size=20, face = "bold", ), strip.background = element_rect(fill="grey75")) +
theme(panel.background = element_rect(fill = "white")) +
theme(plot.background = element_rect(fill = "grey92")) +
theme(legend.position="bottom", legend.background = element_rect(fill="grey92")) +
theme(legend.key = element_blank()) +
theme(panel.grid.minor = element_blank(), panel.grid.major = element_line(colour = "grey75")) +
theme(axis.text.y = element_blank()) +
theme(axis.ticks.length = unit(0, "mm")) +
theme(axis.title.x = element_blank(),axis.title.y = element_blank()) +
theme(legend.box.spacing = unit(1, "mm")) +
facet_wrap(~ cible)
Here my result:
After several hours of serach, I didn't find a solution to create what I want. The exact same pie chart but with personalised ticks like that :
With these particular conditions :
- do not change the direction of the data in the pie chart, I want it like exactly this
- if possible (but if not possible, it's okay), I would like the ticks' labels not superposed with the axis.
If someone can help me, I would really appreciate.
Here's one solution:
ggplot(data = activite %>%
group_by(cible) %>%
arrange(desc(nb)) %>%
mutate(axis.label = cumsum(nb),
axis.position = cumsum(pc)/100) %>%
mutate(axis.label = ifelse(pc == min(pc),
paste(axis.label, "0", sep = "-"),
axis.label)),
aes(x = 1, y = nb, fill = label )) +
geom_segment(aes(x = 1, xend = 1.6, y = axis.position, yend = axis.position),
colour = "grey75") +
geom_vline(xintercept = 1.6, colour = "grey75") +
geom_bar(stat = "identity", position = position_fill(reverse = T), width = 1) +
coord_polar(theta= "y", start = 0, direction = 1) +
labs(fill="") +
scale_fill_manual(values=as.character(couleurs$html[1:nrow(activite)]), labels=paste(activite$label,"\t\t\t",sep="")) +
geom_text(aes(label = paste(pc,"%", sep=" ")), size=4, colour = "white",
fontface = "bold", position = position_fill(vjust = 0.5, reverse = T)) +
geom_text(aes(x = 1.7, label = axis.label), size = 3,
position = position_fill(reverse = T)) +
theme(strip.text = element_text(size=20, face = "bold", ), strip.background = element_rect(fill="grey75")) +
theme(panel.background = element_rect(fill = "white")) +
theme(plot.background = element_rect(fill = "grey92")) +
theme(legend.position="bottom", legend.background = element_rect(fill="grey92")) +
theme(legend.key = element_blank()) +
theme(panel.grid = element_blank()) +
theme(axis.text = element_blank()) +
theme(axis.ticks = element_blank()) +
theme(axis.title = element_blank()) +
theme(legend.box.spacing = unit(1, "mm")) +
facet_wrap(~ cible)
Explanation:
The sequence in your labels went clockwise, while the direction of the polar coordinates went counter-clockwise. That makes labelling rather troublesome. I switched the direction for polar coordinates, & added reverse = T inside the position adjustment calls for the geoms.
It's hard to assign different axis breaks to different facets of the same plot, so I didn't. Instead, I modified the data to include calculated axis labels / margin positions, added margins via geom_segment / geom_vline, & hid the axis text / ticks in theme().