geom-bar

Specific spaces between bars in a barplot - ggplot2 - R

不想你离开。 提交于 2019-11-29 10:50:55
I have a simple bargraph like the following a<-data.frame(x=c("total","male","female","low education", "mid education","high education","working","not working"), y=c(80,30,50,20,40,20,65,35)) a$x<-as.character(a$x) a$x<-factor(a$x,levels=unique(a$x)) ggplot(a,aes(x,y)) + geom_bar(stat="identity",fill="orange",width=0.4) + coord_flip() + theme_bw() Now , because the levels of the x axis (flipped and now seems like y ) have a relation with each other e.g male and female represent sex breakdown , working and not working represent another breakdown etc., I want the axis to leave some space between

How to make variable bar widths in ggplot2 not overlap or gap

℡╲_俬逩灬. 提交于 2019-11-28 20:48:04
geom_bar seems to work best when it has fixed width bars - even the spaces between bars seem to be determined by width, according to the documentation . When you have variable widths, however, it does not respond as I would expect, leading to overlaps or gaps between the different bars (as shown here ). To see what I mean, please try this very simple reproducible example: x <- c("a","b","c") w <- c(1.2, 1.3, 4) # variable widths y <- c(9, 10, 6) # variable heights ggplot() + geom_bar(aes(x = x, y = y, width = w, fill=x), stat="identity", position= "stack") What I really want is for the

ggplot barplot : How to display small positive numbers with log scaled y-axis

寵の児 提交于 2019-11-28 12:26:43
问题 Main issue: I want to display the data from 0 to 1.0 as an upward bar (starting from 0) but do not want the intervals to be equally spaced but log spaced. I am trying to display the column labeled "mean" in the dataset below as a bar plot in ggplot but as the numbers are very small, I would like to show the y-axis on a log scale rather than log transform the data itself. In other words, I want to have upright bars with y-axis labels as 0, 1e-8, 1e-6 1e-4 1e-2 and 1e-0 (i.e. from 0 to 1.0 but

Grouped bar graph custom colours

核能气质少年 提交于 2019-11-28 11:48:54
I have the following data and wish to create a grouped bar graph like so: data<-as.data.frame(c("a","b","c","a","b","c")) colnames(data)<-"Y" data$X<-c("x","x","x","y","y","y") data$Z<-c(1,2,3,1,2,3) ggplot(data, aes(x=X, y=Z, fill=Y) + geom_bar(stat="identity", colour="black", position="dodge", size=0.25, width=0.8, alpha=0.8) + scale_fill_manual(values=c("red","red","red","blue","blue","blue")) In the last line of the code I wish to change the colours of the bars - I would like that all of the bars of group "x" be coloured red and the bars of group "y" to be coloured blue. However as the

How to add custom labels from a dataset on top of bars using ggplot/geom_bar in R?

回眸只為那壹抹淺笑 提交于 2019-11-28 05:26:54
问题 I have the attached datasets and use this R code to plot the data: plotData <- read.csv("plotdata.csv") ix <- 1:nrow(plotData) long <- melt(transform(plotData, id = ix), id = "id") # add id col; melt to long form ggp2 <- ggplot(long, aes(id, value, fill = variable))+geom_bar(stat = "identity", position = "dodge")+ scale_x_continuous(breaks = ix) + labs(y='Throughput (Mbps)',x='Nodes') + scale_fill_discrete(name="Legend", labels=c("Inside Firewall (Dest)", "Inside Firewall (Source)", "Outside

Specific spaces between bars in a barplot - ggplot2 - R

此生再无相见时 提交于 2019-11-28 04:28:41
问题 I have a simple bargraph like the following a<-data.frame(x=c("total","male","female","low education", "mid education","high education","working","not working"), y=c(80,30,50,20,40,20,65,35)) a$x<-as.character(a$x) a$x<-factor(a$x,levels=unique(a$x)) ggplot(a,aes(x,y)) + geom_bar(stat="identity",fill="orange",width=0.4) + coord_flip() + theme_bw() Now , because the levels of the x axis (flipped and now seems like y ) have a relation with each other e.g male and female represent sex breakdown

ggplot2: reorder bars from highest to lowest in each facet [duplicate]

让人想犯罪 __ 提交于 2019-11-28 00:19:09
This question already has an answer here: ggplot bar plot with facet-dependent order of categories 4 answers In the df below, I want to reorder bars from highest to lowest in each facet I tried df <- df %>% tidyr::gather("var", "value", 2:4) ggplot(df, aes (x = reorder(id, -value), y = value, fill = id))+ geom_bar(stat="identity")+facet_wrap(~var, ncol =3) It gave me It didn't order the bars from highest to lowest in each facet. I figured out another way to get what I want. I had to plot each variable at a time, then combine all plots using grid.arrange() #I got this function from @eipi10's

Change bar plot colour in geom_bar with ggplot2 in r

拥有回忆 提交于 2019-11-27 19:03:14
I have the following in order to bar plot the data frame. c1 <- c(10, 20, 40) c2 <- c(3, 5, 7) c3 <- c(1, 1, 1) df <- data.frame(c1, c2, c3) ggplot(data=df, aes(x=c1+c2/2, y=c3)) + geom_bar(stat="identity", width=c2) + scale_fill_manual(values=c("#FF6666")) I end up having only grey bars: Grey bars for bar plot I would like to change the color of the bar. I already tried different scale_fill_manual from http://www.cookbook-r.com/Graphs/Colors_(ggplot2)/ but still have grey bars. Thank you for your help. If you want all the bars to get the same color ( fill ), you can easily add it inside geom

ggplot replace count with percentage in geom_bar

梦想与她 提交于 2019-11-27 14:02:20
I have a dataframe d : > head(d,20) groupchange Symscore3 1 4 1 2 4 2 3 4 1 4 4 2 5 5 0 6 5 0 7 5 0 8 4 0 9 2 2 10 5 0 11 5 0 12 5 1 13 5 0 14 4 1 15 5 1 16 1 0 17 4 0 18 1 1 19 5 0 20 4 0 That I am plotting with: ggplot(d, aes(groupchange, y=..count../sum(..count..), fill=Symscore3)) + geom_bar(position = "dodge") In this way each bar represents its percentage on the whole data. Instead I would like that each bar represents a relative percentage; i.e. the sum of the bar in obtained with groupchange = k should be 1 . First summarise and transform your data: library(dplyr) d2 <- d %>% group_by

geom_text how to position the text on bar as I want?

雨燕双飞 提交于 2019-11-27 11:49:24
I would like to adjust the text on the barplot. I tried to adjust hjust/vjust to display as I like it but it seems like it's not working properly. ggplot(data) + geom_bar(aes(name, count, fill = week), stat='identity', position = 'dodge') + geom_text(aes(name,count, label=count),hjust=0.5, vjust=3, size=2, position = position_dodge(width = 1)) + coord_flip() So I would like the numbers to situate on each bar, in the middle, at the right-edge so it's readable without overlapping like the last parts. Edit: The easier solution to get hjust / vjust to behave intelligently is to add the group