I have made a barplot similar to the one below using ggplot2.
I cannot get the percentages on top of the bars to be centered and not overlapping of other bars and numbers. Sample code is below.
library(tidyverse)
cat1=c("cat1","cat1","cat1","cat1","cat1","cat1","cat1","cat1","cat1","cat1","cat1","cat1",
"cat2","cat2","cat2","cat2","cat2","cat2","cat2","cat2","cat2","cat2","cat2","cat2",
"cat3","cat3","cat3","cat3","cat3","cat3","cat3","cat3","cat3","cat3","cat3","cat3",
"cat4","cat4","cat4","cat4","cat4","cat4","cat4","cat4","cat4","cat4","cat4","cat4")
cat2=c("c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12",
"c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12",
"c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12",
"c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12")
count1=round(rnorm(48,10))
fakeperc=rnorm(48,9)
df1=cbind(count1,fakeperc)
df2=cbind(cat1,cat2)
finaldf=as.data.frame(cbind(df1,df2))
finaldf$cat1=as.factor(finaldf$cat1)
finaldf$fakeperc=as.numeric(finaldf$fakeperc)
#finaldf$cat1=factor(finaldf$cat1,levels = c("cat1","cat2","cat3","cat4"))
finaldf$cat2 = factor(finaldf$cat2,
levels = c("c1","c2","c3","c4","c5","c6","c7","c8","c9","c10","c11","c12"))
a=ggplot(data=finaldf,aes(x=cat1, y=count1,
fill=cat2,group=cat2)) +
geom_bar(stat='identity',color='black',width=.65,position=position_dodge(width=.9))+
scale_y_discrete(limits=0:50,breaks=c(0,10,20,30,40,50))+
scale_fill_brewer(palette="Set3") +
theme_classic() +
geom_text(data = finaldf,
aes(x=cat1,y=count1,group=cat2,
label=format(paste(round(fakeperc),"%",sep = ""))),inherit.aes = F,
color='black',position=position_dodge(.9),vjust=-.5,size=3)
a
When trying to add either nudge_y or nudge_x to the geom_text call, nothing happens. I suspect this is because there is already a position_dodge call. I am open any and all solutions to make these percentages non-overlapping and legible.
What do you think of this?
# I think you meant count1 to be numeric
finaldf$count1 <- as.numeric(finaldf$count1)
ggplot(data = finaldf,
aes(x = cat1,
y = count1,
fill = cat2,
group = cat2)) +
geom_col(color = 'black',
width = 0.65,
position = position_dodge(width = 0.9)) +
geom_text(data = finaldf,
aes(x = cat1,
y = count1,
group = cat2,
label = scales::percent(fakeperc/100, accuracy = 0.01)),
inherit.aes = FALSE,
color = 'black',
position = position_dodge(0.9),
hjust = -0.1,
size = 3) +
scale_y_continuous(limits = c(0,50), breaks = c(0,10,20,30,40,50)) +
scale_fill_brewer(palette = "Set3") +
theme_classic() +
coord_flip()
I cleaned up a bit the code (according to my taste)
I changed scale_y_numeric to scale_y_continuous (since count1 should be numeric)
I used coord_flip() to make it more readable
I used scales::percent to write percentage numbers
(don't know why you set up limits from 0 to 50 but I left them as I suppposed they were intended)
If you don't want to use coor_flip:
finaldf$count1 <- as.numeric(finaldf$count1)
ggplot(data = finaldf,
aes(x = cat1,
y = count1,
fill = cat2,
group = cat2)) +
geom_col(color = 'black',
width = 0.65,
position = position_dodge(width = 0.9)) +
geom_text(data = finaldf,
aes(x = cat1,
y = count1,
group = cat2,
label = scales::percent(fakeperc/100, accuracy = 0.01)),
inherit.aes = FALSE,
color = 'black',
position = position_dodge(0.9),
hjust = -0.1,
angle = 90,
size = 3) +
scale_y_continuous(limits = c(0,50), breaks = c(0,10,20,30,40,50)) +
scale_fill_brewer(palette = "Set3") +
theme_classic()
Is this what you are looking for:
library(ggplot2)
#Code
ggplot(data=finaldf,aes(x=cat2, y=count1,
fill=cat2,group=cat2)) +
geom_bar(stat='identity',color='black',
position=position_dodge(width=1))+
scale_fill_brewer(palette="Set3") +
theme_bw() +
geom_text(aes(x=cat2,y=count1,group=cat2,
label=format(paste(round(fakeperc),"%",sep = ""))),inherit.aes = F,
color='black',position=position_dodge(1),
size=3,vjust=-0.5)+
facet_wrap(.~cat1,scales = 'free_x',nrow = 1,strip.position = 'bottom')+
theme(axis.text.x = element_blank(),
axis.ticks.x = element_blank(),
legend.position = 'top',
strip.background = element_blank(),
panel.spacing = unit(2, "lines"),
panel.grid = element_blank())+
guides(fill = guide_legend(nrow = 1))
Output:
Related
I am trying to create a plot like the one below. I'd like the order the points in each category in such a way that they form an s-shape. Is it possible to do this in ggplot?
Similar data available here
What I have so far:
somatic.variants <- read.delim("data/Lawrence.S2.txt", stringsAsFactors=T)
cancer_rates <- tapply(somatic.variants$logn_coding_mutations, somatic.variants$tumor_type, median)
cancer_rates <- cancer_rates[order(cancer_rates, decreasing=F)]
somatic.variants$tumor_type <- factor(somatic.variants$tumor_type, levels = names(cancer_rates))
library(ggplot2)
library(GGally)
ggplot(data = somatic.variants,
mapping = aes(x = tumor_type,
y = log10(n_coding_mutations))) +
geom_point(position = position_dodge2()) +
scale_x_discrete(position = "top") +
scale_y_continuous(labels = c(0,10,100,1000,10000), expand = c(0,0)) +
geom_stripped_cols() +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 315, hjust = 1, size = 12),
panel.grid = element_blank()) +
labs(y = "Coding mutations count") +
stat_summary(fun = median,
geom="crossbar",
size = 0.25,
width = 0.9,
group = 1,
show.legend = FALSE,
color = "#FF0000")
This could be achieved by
grouping the data by x-axis categories
arranging by the y-axis value
which ensures that the points are plotted in ascending order of the values for each category.
somatic.variants <- read.delim("https://gist.githubusercontent.com/wudustan/57deecdaefa035c1ecabf930afde295a/raw/1594d51a1e3b52f674ff746caace3231fd31910a/Lawrence.S2.txt", stringsAsFactors=T)
cancer_rates <- tapply(somatic.variants$logn_coding_mutations, somatic.variants$tumor_type, median)
cancer_rates <- cancer_rates[order(cancer_rates, decreasing=F)]
somatic.variants$tumor_type <- factor(somatic.variants$tumor_type, levels = names(cancer_rates))
library(ggplot2)
library(GGally)
library(dplyr)
somatic.variants <- somatic.variants %>%
group_by(tumor_type) %>%
arrange(n_coding_mutations)
ggplot(data = somatic.variants,
mapping = aes(x = tumor_type,
y = log10(n_coding_mutations))) +
geom_point(position = position_dodge2(.9), size = .25) +
scale_x_discrete(position = "top") +
scale_y_continuous(labels = c(0,10,100,1000,10000), expand = c(0,0)) +
geom_stripped_cols() +
theme_bw() +
theme(axis.title.x = element_blank(),
axis.text.x = element_text(angle = 315, hjust = 1, size = 12),
panel.grid = element_blank()) +
labs(y = "Coding mutations count") +
stat_summary(fun = median,
geom="crossbar",
size = 0.25,
width = 0.9,
group = 1,
show.legend = FALSE,
color = "#FF0000")
#> Warning: Removed 29 rows containing non-finite values (stat_summary).
I would like to get the same graph given by this code:
library(ggplot2)
name <- c("A","A","A","A","B","B","B","B")
size <- c("small","small","tall","tall","small","small","tall","tall")
flag <- c(0,1,0,1,0,1,0,1)
quantity <- c(26,13,12,4,19,14,13,5)
df <- data.frame(name,size,flag,quantity)
ggplot(data = df, mapping = aes(x = name, y = quantity)) +
geom_bar(aes(fill = size), position = "dodge", stat = "identity")
Except that I would like to have the bars split according to the variable flag. the ideal would be to have a different shade of color for the portion of the bars that corresponds to flag = 0.
I also need to have a legend for the flag variable.
Using different alpha-values for the flag:
This adds different transparency values for the flag. However, all bars are dodged.
ggplot(data = df, mapping = aes(x = name, y = quantity)) +
geom_bar(aes(fill = size, alpha = factor(flag)), position = "dodge", stat = "identity") +
scale_alpha_manual("flag", values = c(0.3, 1))
A combination of facet_wrap with an interaction on the x-axis (name, size):
Keeps the stacking of the bars for the same size, workaround is needed to have a nice x-axis.
ggplot(data = df, mapping = aes(x = interaction(name, size), y = quantity)) +
geom_bar(aes(fill = size, alpha = factor(flag)),
position = "stack", stat = "identity") +
scale_alpha_manual("flag", values = c(0.3, 1)) +
facet_wrap(~ name, strip.position = "bottom", scales = "free_x") +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
axis.title.x = element_blank(),
strip.background = element_blank())
Interaction with size:
With specifying a scale_fill_manual you can assign different colors to the different combinations of size and flag.
ggplot(data = df, mapping = aes(x = name, y = quantity)) +
geom_bar(aes(fill = interaction(size, flag)), position = "dodge", stat = "identity")
library(ggplot2)
df1 <- data.frame(name = c("A","A","A","A","B","B","B","B"),
size = c("small","small","tall","tall","small","small","tall","tall"),
flag = c(0,1,0,1,0,1,0,1),
quantity = c(26,13,12,4,19,14,13,5))
ggplot(data = df1, mapping = aes(x = name, y = quantity)) +
geom_bar(aes(fill = size), alpha = ifelse(flag==0, 0.6, 1),
position = "dodge", stat = "identity")
Adding on top of #kath answer:
library(grid)
library(ggplot2)
gg_color_hue <- function(n) {
hues = seq(15, 375, length = n + 1)
hcl(h = hues, l = 65, c = 100)[1:n]
}
mycols <- gg_color_hue(length(unique(interaction(df$size, df$flag)))/2)
ggplot(data = df, mapping = aes(x = interaction(name, size), y = quantity)) +
geom_bar(aes(fill = interaction(factor(size), factor(flag))),
position = "stack", stat = "identity") +
scale_fill_manual(name = "Size and Flag",
values = c("small.0" = alpha(mycols[1], 3/5),
"tall.0" = alpha(mycols[2], 3/5),
"small.1" = alpha(mycols[1], 1),
"tall.1" = alpha(mycols[2], 1)),
labels = c("Size: small and Flag: 0",
"Size: tall and Flag: 0",
"Size: small and Flag: 1",
"Size: tall and Flag: 1")) +
facet_wrap(~ name, strip.position = "bottom", scales = "free_x") +
theme(axis.ticks.x = element_blank(),
axis.text.x = element_blank(),
strip.background = element_blank(),
panel.spacing = unit(-1.25, "lines")) +
xlab("name")
I can't seem to find a way to get the text labels on this (dodged) geom_col to line up according to their respective columns.
I have tried numerous suggestions solutions on SO and other sites, and this is the closest I could get:
How do I fix this?
Code:
ggplot(leads[leads$key_as_string <= max(leads$key_as_string) - 1, ], aes(fill = type)) +
geom_col(aes(x = key_as_string, y = doc_count),
colour = "black",
position = position_dodge(1)) +
scale_y_continuous(limits = c(0, max(leads$doc_count))) +
geom_text(aes(x = key_as_string, y = doc_count, label = doc_count, group = key_as_string),
hjust = 0.5,
vjust = -0.5,
size = 3,
colour = "black",
position = position_dodge(1)) +
theme(panel.grid.minor.x = element_blank(),
panel.grid.major.x = element_blank(),
axis.text = element_text(colour = "black"))
As per my comment, group = key_as_string is the culprit here. The code is essentially telling ggplot to keep both labels with the same key_as_string value in the same group, negating the dodge command.
Illustration with the diamonds dataset below. We can see that removing the group aesthetic mapping changes the labels' positions:
p <- ggplot(diamonds %>%
filter(cut %in% c("Fair", "Good")) %>%
group_by(cut, clarity) %>%
summarise(carat = mean(carat)),
aes(clarity, carat, fill = cut, label = round(carat, 2))) +
geom_col(position = position_dodge(1))
gridExtra::grid.arrange(
p + geom_text(position = position_dodge(1), aes(group = clarity)),
p + geom_text(position = position_dodge(1)),
ncol = 1
)
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().
I am trying to plot a stacked barchart using geom_bar and label each category with its value within the barchart. Because some categories have small values, the height of the corresponding segment of the barchart is sometimes small. So, I am trying to adjust the size of the labels using geom_text.
I have tried to define a vector, called size, that varies according to the value of the variable I am trying to plot but,although the size of the labels does vary between categories, it does not seem to be related to the values. Also, I am not sure why I am getting a legend for my label size on the right-hand side of the graph.
Here is a stripped version of the code I am using:
library(ggplot2)
library(plyr)
library(scales)
Var1 = as.factor(rep(c("A", "B", "C", "D", "E", "F"),2))
Var2 = as.factor(rep(c("Y1","Y2"),each=6))
Freq = c(0.4, 0.1, 0.3, 0.1, 0.05, 0.05,0.2,0.2,0.3,0.2,0.05,0.05)
Data = data.frame(Var1, Var2, Freq)
Data <- ddply(Data, .(Var2), mutate, y = cumsum(Freq)-Freq/2)
size = ifelse(Data$Freq > 0.05, 10, 3)
label = paste(round(Data$Freq*100,0),"%", sep = "")
p = ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) +
geom_bar(stat = "identity",position = "fill", width = 1) +
scale_fill_brewer(palette = 3) +
facet_grid(facets = . ~ Var2) +
geom_text(aes(y = y, label = label,
position ="identity", face = "bold"), size = size, hjust=0.5, vjust=0.5) +
xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') +
theme(axis.text.y = element_text(size=14,face="bold"),
panel.background = element_blank(),
plot.title = element_text(size = 20, colour = "black", face = "bold"))
p
As far as I can see, the issue is caused by the facets since this slightly simplified version (i.e. without the facets) works fine:
library(ggplot2)
library(plyr)
library(scales)
Var1 = as.factor(c("A", "B", "C", "D", "E", "F"))
Freq = c(0.4, 0.1, 0.3, 0.1, 0.05, 0.05)
y = cumsum(Freq)-Freq/2
Data = data.frame(Var1, Freq, y)
size = ifelse(Data$Freq > 0.05, 10, 3)
label = paste(round(Data$Freq*100,0),"%", sep = "")
p = ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) +
geom_bar(stat = "identity",position = "fill", width = 1) +
scale_fill_brewer(palette = 3) +
geom_text(aes(y = y, label = label,
position ="identity", face = "bold"), size = size, hjust=0.5, vjust=0.5) +
xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') +
theme(axis.text.y = element_text(size=14,face="bold"),
panel.background = element_blank(),
plot.title = element_text(size = 20, colour = "black", face = "bold"))
p
By adding a size to your data frame and moving those parameters within your geom_text aes()` this seems to plot ok for me. I think..
Data$size <- size
p <- ggplot(data = Data, aes(x = factor(''), y = Freq, fill = Var1)) +
geom_bar(stat = "identity",position = "fill", width = 1) +
scale_fill_brewer(palette = 3) +
geom_text(aes(y = y, label = label,
position ="identity", face = "bold", size = size), hjust=0.5,
vjust=0.5) +
xlab('') + ylab('') + labs(fill = '') + ggtitle('Example') +
theme(axis.text.y = element_text(size=14,face="bold"),
panel.background = element_blank(),
plot.title = element_text(size = 20, colour = "black", face = "bold")) +
facet_grid(facets = . ~ Var2) +
guides(size=FALSE)
Also if you add + guides(size=FALSE) to the end as I have done this will remove your size legend.
My explanation for this, might be wrong, is that once you facet you were still providing a full length size and not allowing facet to chop the size data according to Var2 .
I think your problem with the sizing is that size you only have two sizes you are getting big differences (there must be some relative scaling maybe), perhaps add + scale_size(range=c(6,10)) and play with this to get something more appropriate? The 6,10 size range looks much better to me.