Customize legend labels barchart - r
I'm currently working on a sales dataset, and I have been trying to make a barchart, showcasing the units sold by each country, with the colouring of the charts being the number of inhabitants in each country.
so far I have used the following code to get the chart
ChartProductsbyCountry <- ggplot(Dataframe10, aes(x=Country, y=SumAct_U)) + geom_col(aes(fill=Inhabitants_2019)) + theme(axis.text.x = element_text(angle = 90, size = 10))
library(scales)
ChartProductsbyCountry + scale_y_continuous(labels = comma)
This gives me the following chart
Barchart
I'm already very satisfied with it, however I would like to change some things but don't know how:
on the right side, showing the "colouring labels/legend", it does not show the actual numbers but rather 2e+07, 4e+07, etc... how can I change it to showing Numbers instead? And as for the y-axis, how should I change my code to have ticks going from 0 to 1.250.000,00 at every 125.000 (Units) (so starting with 0, then 125.000, then 250.000, then 375.000, ...)?
OP, as mentioned by #stefan in the comment, you can use the breaks= argument to control spacing of your ticks on the axis. To format the colorbar legend items, you can use scale_fill_continuous() in the same manner you did for the y axis. Here's an example:
library(ggplot2)
library(scales)
set.seed(8675309)
df <- data.frame(x=LETTERS, y=sample(0:20, replace=T, size=26) * 1000000)
p <-
ggplot(df, aes(x,y, fill=y)) + geom_col() +
scale_y_continuous(labels = comma)
p
To set the number formatting in the colorbar, you can use labels = comma within scale_fill_continuous(). To change the ticks on the y axis, you can access that via the breaks= argument. Essentially, send a vector of values to breaks= to have the labels marked where you want. In this case, I'll set it up to make a tick mark every 1 million, and then set the colorbar to use comma format:
ggplot(df, aes(x,y, fill=y)) + geom_col() +
scale_y_continuous(labels = comma, breaks=seq(0, max(df$y), by=1000000)) +
scale_fill_continuous(labels=comma)
Related
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()
X axis labels tied to histogram bars instead of following separate rules
When using a histogram with x as a POSIXct value, I'm not sure how you're supposed to line the ticks up with the binsize of the graph. Setting the tick size to the same as the binsize makes it line a bit off, but the offset adds onto each other until its no longer accurate. bymonth <- ggplot() + scale_x_datetime("", breaks = date_breaks("60 days"), labels = date_format("%m-%y")) + ... lots of geom_rects for background colors ... theme(legend.title = element_blank()) + geom_histogram(data=dat, aes(x = iso, fill = name), binwidth = 30*24*60*60, position = 'dodge') I tried using annotate() as well as experimenting with the spacing of the tick but I think my approach here might be wrong in its own accord This leads to a graph looking something like this Which is quite annoying
Adding different secondary x axis for each facet in ggplot2
I would like to add a different secondary axis to each facet. Here is my working example: library(ggplot2) library(data.table) #Create the data: data<-data.table(cohort=sample(c(1946,1947,1948),10000,replace=TRUE), works=sample(c(0,1),10000,replace=TRUE), year=sample(seq(2006,2013),10000,replace=TRUE)) data[,age_cohort:=year-cohort] data[,prop_works:=mean(works),by=c("cohort","year")] #Prepare data for plotting: data_to_plot<-unique(data,by=c("cohort","year")) #Plot what I want: ggplot(data_to_plot,aes(x=age_cohort,y=prop_works))+geom_point()+geom_line()+ facet_wrap(~ cohort) The plot shows how many people of a particular cohort work at a given age. I would like to add a secondary x axis showing which year corresponds to a particular age for different cohorts.
Since you have the actual values you want to use in your dataset, one work around is to plot them as an additional geom_text layer: ggplot(data_to_plot, aes(x = age_cohort, y = prop_works, label = year))+ geom_point() + geom_line() + geom_text(aes(y = min(prop_works)), hjust = 1.5, angle = 90) + # rotate to save space expand_limits(y = 0.44) + scale_x_continuous(breaks = seq(58, 70, 1)) + # ensure x-axis breaks are at whole numbers scale_y_continuous(labels = scales::percent) + facet_wrap(~ cohort, scales = "free_x") + # show only relevant age cohorts in each facet theme(panel.grid.minor.x = element_blank()) # hide minor grid lines for cleaner look You can adjust the hjust value in geom_text() and y value in expand_limits() for a reasonable look, depending on your desired output's dimensions. (More data wrangling would be required if there are missing years in the data, but I assume that isn't the case here.)
changing ggplot legend unit scale
This question is motivated by a previous post illustrating various ways to change how axes scales are plotted in a ggplot figure, from the default exponential notation to the full integer value (when ones axes values are very large). While I am able to convert the axes scales from exponential notation to full values, I am unclear how one would achieve the same goal for the values appearing in the legend. While I understand that one can manually change the length of the legend scale with "scale_color..." or "scale_fill..." followed by the "limits" argument, this does not appear to be a solution to getting my legend values to show "6000000000" rather than "6e+09" (or "0" rather than "0e+00" for that matter). The following example should suffice. My hope is someone can point out how to implement the 'scales' package to apply for legend scales rather than axes scales. Thanks very much. library(ggplot2) library(scales) Data <- data.frame( pi = c(2,71,828,1828,45904,523536,2874713,52662497,757247093,6999595749), e = c(3,14,159,2653,58979,311599,7963468,54418516,1590576171, 99), face = 1:10) p <- ggplot(data = Data, aes(x=face, y=e, colour = pi)) myplot <- p + geom_point() + scale_y_continuous(labels = comma) + scale_color_gradientn(colours = rainbow(2), limits=c(0,7000000000)) myplot
Use the Comma formatter in scale_color_gradientn by setting labels = comma e.g.: p <- ggplot(data = Data, aes(x=face, y=e, colour = pi)) myplot <- p + geom_point() + scale_y_continuous(labels = comma) + scale_color_gradientn(colours = rainbow(2), limits=c(0,7000000000), labels = comma) myplot
How to change origin line position in ggplot bar graph?
Say I'm measuring 10 personality traits and I know the population baseline. I would like to create a chart for individual test-takers to show them their individual percentile ranking on each trait. Thus, the numbers go from 1 (percentile) to 99 (percentile). Given that a 50 is perfectly average, I'd like the graph to show bars going to the left or right from 50 as the origin line. In bar graphs in ggplot, it seems that the origin line defaults to 0. Is there a way to change the origin line to be at 50? Here's some fake data and default graphing: df <- data.frame( names = LETTERS[1:10], factor = round(rnorm(10, mean = 50, sd = 20), 1) ) library(ggplot2) ggplot(data = df, aes(x=names, y=factor)) + geom_bar(stat="identity") + coord_flip()
Picking up on #nongkrong's comment, here's some code that will do what I think you want while relabeling the ticks to match the original range and relabeling the axis to avoid showing the math: library(ggplot2) ggplot(data = df, aes(x=names, y=factor - 50)) + geom_bar(stat="identity") + scale_y_continuous(breaks=seq(-50,50,10), labels=seq(0,100,10)) + ylab("Percentile") + coord_flip()
This post was really helpful for me - thanks #ulfelder and #nongkrong. However, I wanted to re-use the code on different data without having to manually adjust the tick labels to fit the new data. To do this in a way that retained ggplot's tick placement, I defined a tiny function and called this function in the label argument: fix.labels <- function(x){ x + 50 } ggplot(data = df, aes(x=names, y=factor - 50)) + geom_bar(stat="identity") + scale_y_continuous(labels = fix.labels) + ylab("Percentile") + coord_flip()