geom-bar
ggplot2: Setting geom_bar baseline to 1 instead of zero
You can shift the geom_bar baseline to 1 (instead of zero) as follows: Shift the data by -1, so that ratio=1 becomes zero and is therefore used as the baseline. Add 1 to the y-axis labels so that they reflect the actual data values. dat = data.frame(ratio=-4:11/3, x=1:16) ggplot(dat, aes(x, ratio-1, fill=ifelse(ratio-1>0,”GT1″,”LT1″))) + geom_bar(stat=”identity”) + … Read more
Generate paired stacked bar charts in ggplot (using position_dodge only on some variables)
One workaround would be to put interaction of sample and name on x axis and then adjust the labels for the x axis. Problem is that bars are not put close to each other. ggplot(df, aes(x = as.numeric(interaction(sample,name)), y = count, fill = type)) + geom_bar(stat = “identity”,color=”white”) + scale_x_continuous(breaks=c(1.5,3.5,5.5),labels=c(“oak”,”birch”,”cedar”)) Another solution is to use … Read more
ggplot replace count with percentage in geom_bar
If your goal is visualization in minimal code, use position = “fill” as an argument in geom_bar(). If you want within group percentages, @Jaap’s dplyr answer answer is the way to go. Here is a reproducible example using the above dataset to copy/paste: library(tidyverse) d <- data_frame(groupchange = c(4,4,4,4,5,5,5,4,2,5,5,5,5,4,5,1,4,1,5,4), Symscore3 = c(1,2,1,2,0,0,0,0,2,0,0,1,0,1,1,0,0,1,1,0)) ggplot(d, aes(x = … Read more
geom_text how to position the text on bar as I want?
Edit: The easier solution to get hjust/vjust to behave intelligently is to add the group aesthetic to geom_text and then hjust & position adjust for the group automatically. 1. Vertical Orientation ggplot(data) + geom_bar( aes(x = name, y = count, fill = week, group = week), stat=”identity”, position = ‘dodge’ ) + geom_text( aes(x = … Read more
How to add frequency count labels to the bars in a bar graph using ggplot2? [duplicate]
ggplot(data=diamonds, aes(x=clarity)) + geom_bar() + geom_text(stat=”count”, aes(label=..count..), vjust=-1)