ggplot: same bar widths with different number of bars - r
I would like to have barplots in which the bars have the same width across different plots, no matter how many bars are shown. I do not want to show the plots on the same page, or arrange them with facets, grid.arrange or anything like that, but just have two plots with bars of the same width.
I could do this by just multiplying the width by the number of bars in the plot divided by the number of bars in the plot with the most bars (see example). But it would be more convenient and somewhat cleaner code if I could do this without any computations before the ggplot call.
Is there a way to specify the bar widths in a unit like lines, em, centimeters?
Or can I access the number of levels of the variable mapped to the x-aesthetic in the call to geom_col? (Note the variable mapped to the x-aesthetic changes between plots)
Or is there another simple solution?
ggplot(data.frame(x=factor(1:2), y=4:5), aes(x=x, y=y)) +
geom_col(width=0.7*2/3)
ggplot(data.frame(A=factor(1:3), y=3:5), aes(x=A, y=y)) +
geom_col(width=0.7*3/3)
AFAIK, you can not set an absolute width to geom_col()/geom_bar(), so you'd either have to precalculate the proportions and aspect ratio of the bars or use geom_segment() that takes a size argument that is absolute. These aren't internally parameterised as rectangles and don't take seperate colour and fill arguments though.
library(ggplot2)
library(patchwork)
g1 <- ggplot(data.frame(x=factor(1:2), y=4:5), aes(x=x, y=y, xend = x, yend = 0)) +
geom_segment(size = 20)
g2 <- ggplot(data.frame(A=factor(1:3), y=3:5), aes(x=A, y=y, xend=A, yend = 0)) +
geom_segment(size = 20)
g1 + g2
Related
Is it possible to make a column plot using ggplot in which the column fill is controlled by a third variable?
I have a data frame with three continuous variables (x,y,z). I want a column plot in which x defines the x-axis position of the columns, y defines the length of the columns, and the column colors (function of y) are defined by z. The test code below shows the set up. `require(ggplot2) require(viridis) # Create a dummy data frame x <- c(rep(0.0, 5),rep(0.5,10),rep(1.0,15)) y <- c(seq(0.0,-5,length.out=5), seq(0.0,-10,length.out=10), seq(0.0,-15,length.out=15)) z <- c(seq(10,0,length.out=5), seq(8,0,length.out=10), seq(6,0,length.out=15)) df <- data.frame(x=x, y=y, z=z) pbase <- ggplot(df, aes(x=x, y=y, fill=z)) ptest <- pbase + geom_col(width=0.5, position="identity") + scale_fill_viridis(option="turbo", limits = c(0,10), breaks=seq(0,10,2.5), labels=c("0","2.5","5.0","7.5","10.0")) print(ptest)` The legend has the correct colors but the columns do not. Perhaps this is not the correct way to do this type of plot. I tried using geom_bar() which creates a bars with the correct colors but the y-values are incorrect.
It looks like you have 3 X values that each appear 5, 10, or 15 times. Do you want the bars to be overlaid on top of one another, as they are now? If you add an alpha = 0.5 to the geom_col call you'll see the overlapping bars. Alternatively, you might use dodging to show the bars next to one another instead of on top of one another. ggplot(df, aes(x=x, y=y, fill=z, group = z)) + geom_col(width=0.5, position=position_dodge()) + scale_fill_viridis_c(option="turbo", # added with ggplot 3.x in 2018 limits = c(0,10), breaks=seq(0,10,2.5), labels=c("0","2.5","5.0","7.5","10.0")) Or you might plot the data in order of y so that the smaller bars appear on top, visibly: ggplot(dplyr::arrange(df,y), aes(x=x, y=y, fill=z))+ geom_col(width=0.5, position="identity") + scale_fill_viridis_c(option="turbo", limits = c(0,10), breaks=seq(0,10,2.5), labels=c("0","2.5","5.0","7.5","10.0"))
I solved this by using geom_tile() in place of geom_col().
Ggplot2 Boxplot width setting changes x-axis
I have produced a boxplot with a continuous x-axis unsing geom_boxplot() in ggplot2. However, as there are many boxes they appear as skinny lines. Another stackoverflow chain (see here) suggested using the width= argument to make all the boxes the same width. However, when I use this argument it changes the x-axis and some of the boxes just disappear! For example, take this example dataframe. I apologise for the number of observations this has but I think the quantity has to do with the problem as I couldn't reproduce it with a more simple boxplot: Lat<- c(50.70228,50.70228,50.70228,51.82067,51.82067,51.82067,52.45893,52.45893,52.45893,52.76478,52.76478,52.76478,52.78354,52.78354,52.78354,53.56102,53.56102,53.56102,53.65364,53.65364,53.65364,53.63130,53.63130,53.63130,54.19035,54.19035,54.19035,54.25751,54.25751,54.25751,54.23526,54.23526,54.23526,54.62469,54.62469,54.62469,54.67831,54.67831,54.67831,54.67900,54.67900,54.67900,54.94908,54.94908,54.94908,55.19456,55.19456,55.19456,54.79198,54.79198,54.79198,55.34981,55.34981,55.34981,55.85655,55.85655,55.85655,56.06078,56.06078,56.06078,55.84553,55.84553,55.84553,56.00197,56.00197,56.00197,56.71842,56.71842,56.71842,57.00116,57.00116,57.00116,57.06942,57.06942,57.06942,57.26815,57.26815,57.26815,57.45532,57.45532,57.45532,57.88596,57.88596,57.88596,51.07711,51.07711,51.07711,51.07801,51.07621,51.11159,51.11159,51.11159,52.02484,52.02484,52.02484,52.02581,52.02581,52.02581,52.02685,52.02685,52.02685,52.05353,52.05353,52.05626,52.05353,52.05353,52.05353,52.05353,52.05353,52.05353,51.93541,51.93541,51.93541,51.93541,51.93541,51.93541,51.93541,51.93541,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.90810,52.90810,52.90810,52.90810,52.90810,52.90810,52.78968,52.78778,52.78968,52.78968,52.78881,52.78883,52.78883,52.78883,52.78970,52.78970,52.79506,52.79506,52.79506,53.77270,53.77276,53.77109,53.77109,53.77276,53.76845,53.76845,53.77109,53.76845,53.77109,53.87020,53.87020,53.87020,53.87103,53.88205,53.88205,53.88205,53.88205,53.87701,53.87701,53.87098,53.87098,53.87098,53.86932,53.86932,53.86932,56.51869,56.51869,56.51869,56.55870,56.55870,56.55870,56.55964,56.55964,56.55964,57.51056,57.49542,57.49542,57.50878,57.50878,57.50878,57.45201,57.45477,57.45192,57.45192,57.45192) y <- c(33.45407,21.40954,27.73487,20.38318,26.65483,31.68201,23.95467,20.77363,32.94192,22.71228,25.78824,28.39449,35.60615,24.29325,22.95047,25.65343,30.23262,22.05534,37.20565,35.53812,38.20211,39.38034,35.16619,38.82336,29.72370,38.25754,26.51339,39.38283,29.57483,31.80111,24.52967,34.83037,21.75038,35.50868,39.41830,21.96971,22.82504,32.69746,35.10747,27.75669,34.96690,37.61921,37.17226,20.50448,39.26582,22.08668,28.41502,36.69530,23.69404,23.18052,33.27420,23.04157,33.17285,32.00579,21.83845,22.97143,32.27190,21.53771,38.65481,20.14341,33.62718,39.86755,39.77881,30.59810,27.65909,24.11646,34.56981,29.30249,34.99361,32.39553,28.90443,34.88775,22.77049,36.44468,30.64496,35.81501,31.77673,24.19058,39.36298,21.47219,23.02268,31.37647,27.28457,33.14749,23.20842,39.73427,39.81399,35.51515,24.55080,39.41190,29.59987,38.46791,20.94479,37.22109,26.36060,30.91641,39.25975,39.88288,22.59061,30.24439,21.66110,30.36878,28.76901,38.75561,33.80408,31.05842,26.18921,21.30804,35.02966,33.85981,30.84373,31.67341,35.07605,37.93820,31.30481,21.45117,37.13626,25.70964,25.64736,38.58381,31.24448,26.55902,23.90817,33.70300,26.48909,37.73200,32.52413,22.44440,28.19878,32.46415,25.13711,26.66075,28.16254,20.40673,39.89327,30.83327,32.40196,39.81218,39.80391,21.87316,34.95792,33.38958,38.18441,22.03114,35.64410,34.90643,24.23056,36.66581,29.35813,20.86880,30.02044,36.13727,24.65558,39.43175,29.00154,29.78185,22.89196,37.15204,35.88188,28.73920,28.04934,37.50701,30.36306,28.39842,35.20973,26.54260,29.57763,26.03163,26.90440,27.60110,25.80086,39.98019,21.59970,28.83825,32.01711,20.50812,38.43331,32.41898,27.68722,32.59905,24.18150,29.05701,22.38512,32.93342,37.66694,37.65391,34.19613,23.89985,36.90012,20.74244,27.08511,29.21433,35.83771,35.59557,33.74533,27.08854,38.38994) V3 <-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2) df <- as.data.frame(cbind(Lat, y, as.factor(V3))) head(df) I plot it on a continuous x-axis as so: df_plot <- ggplot(df, aes(x=Lat, y=y, group=Lat))+ geom_boxplot(aes(colour=as.factor(V3)))+ theme_classic() df_plot Which produces: As you can see the boxes are represented as skinny lines. Therefore I tried to use the width= argument as so: df_plot2 <- ggplot(df, aes(x=Lat, y=y, group=Lat))+ geom_boxplot(aes(colour=as.factor(V3)), width=1)+ theme_classic() df_plot2 The output is: The main thing to notice here is that the x-axis range has suddenly changed! Some of the boxes are no longer plotted whilst others seem to be placed at different values of the x-axis. The range of the x-axis should be: range(df$Lat) [1] 50.70228 57.88596 I am completley perplexed as to why the x-axis would change by simply adding the width= argument in geom_boxplot(). I therefore tried to force the limits of the x-axis scale as so: df_plot3 <- ggplot(df, aes(x=Lat, y=y, group=Lat))+ geom_boxplot(aes(colour=as.factor(V3)), width=1)+ xlim(50,58)+ theme_classic() df_plot3 ouput: Please send help!
I think the strange behaviour comes from ggplot trying to automatically dodge your boxplots apart. By setting position = position_dodge(width = 0) the plot seems to be created as expected without changing the placement of boxes along the x-axis. (But gives a warning about overlapping x intervals) Lat<- c(50.70228,50.70228,50.70228,51.82067,51.82067,51.82067,52.45893,52.45893,52.45893,52.76478,52.76478,52.76478,52.78354,52.78354,52.78354,53.56102,53.56102,53.56102,53.65364,53.65364,53.65364,53.63130,53.63130,53.63130,54.19035,54.19035,54.19035,54.25751,54.25751,54.25751,54.23526,54.23526,54.23526,54.62469,54.62469,54.62469,54.67831,54.67831,54.67831,54.67900,54.67900,54.67900,54.94908,54.94908,54.94908,55.19456,55.19456,55.19456,54.79198,54.79198,54.79198,55.34981,55.34981,55.34981,55.85655,55.85655,55.85655,56.06078,56.06078,56.06078,55.84553,55.84553,55.84553,56.00197,56.00197,56.00197,56.71842,56.71842,56.71842,57.00116,57.00116,57.00116,57.06942,57.06942,57.06942,57.26815,57.26815,57.26815,57.45532,57.45532,57.45532,57.88596,57.88596,57.88596,51.07711,51.07711,51.07711,51.07801,51.07621,51.11159,51.11159,51.11159,52.02484,52.02484,52.02484,52.02581,52.02581,52.02581,52.02685,52.02685,52.02685,52.05353,52.05353,52.05626,52.05353,52.05353,52.05353,52.05353,52.05353,52.05353,51.93541,51.93541,51.93541,51.93541,51.93541,51.93541,51.93541,51.93541,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.92425,52.90810,52.90810,52.90810,52.90810,52.90810,52.90810,52.78968,52.78778,52.78968,52.78968,52.78881,52.78883,52.78883,52.78883,52.78970,52.78970,52.79506,52.79506,52.79506,53.77270,53.77276,53.77109,53.77109,53.77276,53.76845,53.76845,53.77109,53.76845,53.77109,53.87020,53.87020,53.87020,53.87103,53.88205,53.88205,53.88205,53.88205,53.87701,53.87701,53.87098,53.87098,53.87098,53.86932,53.86932,53.86932,56.51869,56.51869,56.51869,56.55870,56.55870,56.55870,56.55964,56.55964,56.55964,57.51056,57.49542,57.49542,57.50878,57.50878,57.50878,57.45201,57.45477,57.45192,57.45192,57.45192) y <- c(33.45407,21.40954,27.73487,20.38318,26.65483,31.68201,23.95467,20.77363,32.94192,22.71228,25.78824,28.39449,35.60615,24.29325,22.95047,25.65343,30.23262,22.05534,37.20565,35.53812,38.20211,39.38034,35.16619,38.82336,29.72370,38.25754,26.51339,39.38283,29.57483,31.80111,24.52967,34.83037,21.75038,35.50868,39.41830,21.96971,22.82504,32.69746,35.10747,27.75669,34.96690,37.61921,37.17226,20.50448,39.26582,22.08668,28.41502,36.69530,23.69404,23.18052,33.27420,23.04157,33.17285,32.00579,21.83845,22.97143,32.27190,21.53771,38.65481,20.14341,33.62718,39.86755,39.77881,30.59810,27.65909,24.11646,34.56981,29.30249,34.99361,32.39553,28.90443,34.88775,22.77049,36.44468,30.64496,35.81501,31.77673,24.19058,39.36298,21.47219,23.02268,31.37647,27.28457,33.14749,23.20842,39.73427,39.81399,35.51515,24.55080,39.41190,29.59987,38.46791,20.94479,37.22109,26.36060,30.91641,39.25975,39.88288,22.59061,30.24439,21.66110,30.36878,28.76901,38.75561,33.80408,31.05842,26.18921,21.30804,35.02966,33.85981,30.84373,31.67341,35.07605,37.93820,31.30481,21.45117,37.13626,25.70964,25.64736,38.58381,31.24448,26.55902,23.90817,33.70300,26.48909,37.73200,32.52413,22.44440,28.19878,32.46415,25.13711,26.66075,28.16254,20.40673,39.89327,30.83327,32.40196,39.81218,39.80391,21.87316,34.95792,33.38958,38.18441,22.03114,35.64410,34.90643,24.23056,36.66581,29.35813,20.86880,30.02044,36.13727,24.65558,39.43175,29.00154,29.78185,22.89196,37.15204,35.88188,28.73920,28.04934,37.50701,30.36306,28.39842,35.20973,26.54260,29.57763,26.03163,26.90440,27.60110,25.80086,39.98019,21.59970,28.83825,32.01711,20.50812,38.43331,32.41898,27.68722,32.59905,24.18150,29.05701,22.38512,32.93342,37.66694,37.65391,34.19613,23.89985,36.90012,20.74244,27.08511,29.21433,35.83771,35.59557,33.74533,27.08854,38.38994) V3 <-c(1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2) library(ggplot2) df <- as.data.frame(cbind(Lat, y, as.factor(V3))) df_plot <- ggplot(df) + geom_boxplot(aes(colour=as.factor(V3), x=Lat, y=y, group=as.factor(Lat)), position=position_dodge(width = 0), width=1) + theme_classic()
Remove space between bars in ggplot2 geom_bar
I am looking to "dodge" the bars of a barplot together. The following R code leaves white space between the bars. Other answers like this one show how to accomplish this for the bars part of a group, but that does not seem to apply for distinct bars per factor on the x axis. require(ggplot2) dat <- data.frame(a=c("A", "B", "C"), b=c(0.71, 0.94, 0.85), d=c(32, 99, 18)) ggplot(dat, aes(x= a, y = b, fill=d, width = d/sum(d))) + geom_bar(position=position_dodge(width = 0.1), stat="identity") Playing with the width variable changes the appearance, but it does not seem possible to get the bars to sit side by side while still retaining their meaningful difference in width (in this graph redundantly represented by the fill colour too).
I would generate my x-positions and widths first, then pass them in to the aesthetics and override to make your factor labels: First, store the width dat$width <- dat$d / sum(dat$d) Then, assuming that your data.frame is in the order you want it plotted, you can set the location as the cumulative sum of the widths. Note, however, that that cumulative sum is where you want the right edge of the bar to be, so to get the center you need to subtract half of the width: dat$loc <- cumsum(dat$width) - dat$width/2 Then, pass it all in to the ggplot call, setting your labels explictly: ggplot(dat, aes(x= loc, y = b, fill=d, width = width)) + geom_bar(stat="identity") + scale_x_continuous(breaks = dat$loc , labels = dat$a) gives I am not sure about the advisability of this appproach, but this should get the job done.
It is possible by using a continuous x axis and relabel it. ggplot(dat, aes(x=cumsum(d/sum(d))) - d/sum(d)/2, y = b, fill=d, width=d/sum(d))) + geom_bar(stat="identity", position=position_dodge()) + scale_x_continuous(breaks=cumsum(dat$d/sum(dat$d)) - dat$d/sum(dat$d)/2, labels=dat$a) Or isn't this what you where looking for
ggplot2 bar plot: hjust depending on bar and label size
Being new to R, I produced very simple horizontal bar plots using ggplot2 and coord_flip(). Notably, I insert the values of the x variable at the left side of the bar by default (or at the right side if the label does not fit) using the following command: geom_text(aes(x=TYPE, y=COUNT, ymax=COUNT, label=COUNT, hjust=ifelse(COUNT>1000, 1.5, -0.3)), size=3.5, position = position_dodge(width=0.8)) The problem is that, depending on the data-sets, the x values can vary significantly (e.g. dataset_1 x values can be between 1 to 200; dataset_2 x values can be between 10,000 to 100,000; ...), which causes the label of the shortest bar to be misplaced with the ifelse statement I am using (see brown bar in figure A below). In this case I cannot just use a constant COUNT>1000 condition for all the datasets. Figure A: I could modify manually the value of the hjust=ifelse(COUNT>1000,...statement for each dataset. But I was wondering if it is possible to automatically move the label outs of the bar if it does not fit between the axis and the top of the bar without modifying the value of the ifelse condition for each dataset, like in figure B below. Figure B : EDIT Workaround (not perfect but better): Placing the label at the right of the bar if the value is less than 5% of the maximum value MAXI <- max(data[,2]) geom_text(aes(x=TYPE, y=COUNT, ymax=COUNT, label=COUNT, hjust=ifelse((COUNT/MAXI)<0.05, -0.3, 1.3)))
Having some labels outside the bars and some inside can distort the visual encoding of magnitude as the length of the bar. Another option is to put the values in the middle of the bar but set geom_text to skip values that are small relative to the maximum bar. Or, if you want to include text for all the bar values added, you can put them below the bars in order to keep a clean visual pattern for the bar lengths. Examples of both options are below: # Fake data dat = data.frame(x = LETTERS[1:5], y=c(432, 1349, 10819, 5489, 12123)) ggplot(dat, aes(x, y, fill=x)) + geom_bar(stat="identity") + geom_text(aes(label=ifelse(y < 0.05*max(dat$y), "", format(y, big.mark=",")), y=0.5*y), colour="white") + coord_flip(xlim=c(0.4,5.6), ylim=c(0, 1.03*max(dat$y)), expand=FALSE) + guides(fill=FALSE) ggplot(dat, aes(x, y, fill=x)) + geom_hline(yintercept=0, lwd=0.3, colour="grey40") + geom_bar(stat="identity") + geom_text(aes(label=format(y, big.mark=","), y=-0.01*max(dat$y)), size=3.5, hjust=1) + coord_flip(ylim = c(-0.04*max(dat$y), max(dat$y))) + guides(fill=FALSE)
How to control ggplot's plotting area proportions instead of fitting them to devices in R?
By default, each plot in ggplot fits its device. That's not always desirable. For instance, one may need to make tiles in geom_tile to be squares. Once you change the device or change the number of elements on x/y-axis, the tiles are no longer squares. Is it possible to set hard proportions or size for a plot and fit the plot in its device's window (or make the device width and height proportional to those of the plot)?
You can specify the aspect ratio of your plots using coord_fixed(). > library(ggplot2) > df <- data.frame( + x = runif(100, 0, 5), + y = runif(100, 0, 5)) If we just go ahead and plot these data then we get a plot which conforms to the dimensions of the output device. > ggplot(df, aes(x=x, y=y)) + geom_point() If, however, we use coord_fixed() then we get a plot with fixed aspect ratio (which, by default has x- and y-axes of same length). The size of the plot will be determined by the shortest dimension of the output device. > ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed() Finally, we can adjust the fixed aspect ratio by specifying an argument to coord_fixed(), where the argument is the ratio of the length of the y-axis to the length of the x-axis. So, to get a plot that is twice as tall as it is wide, we would use: > ggplot(df, aes(x=x, y=y)) + geom_point() + coord_fixed(2)
A cleaner way is to use the theme(aspect.ratio) argument e.g. library(ggplot2) d <- data.frame(x=rnorm(100),y=rnorm(100)*1000) ggplot(d,aes(x,y))+ geom_point() + theme(aspect.ratio=1/10) #Long and skinny coord_fixed() sets the ratio of x/y coordinates, which isn't always the same thing (e.g. in this case, where the units of x and y are very different.
Here's an easy device to treat your plot with respect, library(ggplot2) p = qplot(1:10, (1:10)^3) g = ggplotGrob(p) g$respect = TRUE library(grid) grid.draw(g)