geom-bar

geom_bar + geom_line: with different y-axis scale?

做~自己de王妃 提交于 2019-11-27 07:09:42
问题 Is there any way to plot geom_bar with geom_line like the following chart. I have come up with the two separate charts. How to combine them with two different axes on the left and right sides respectively. library(ggplot2) temp = data.frame(Product=as.factor(c("A","B","C")), N = c(17100,17533,6756), n = c(5,13,11), rate = c(0.0003,0.0007,0.0016), labels = c(".03%",".07%",".16%")) p1 = ggplot(data = temp, aes(x=Product,y=N))+ geom_bar(stat="identity",fill="#F8766D")+geom_text(aes(label=n,col=

How to set Bin Width With geom_bar stat=“identity” in a time Series plot?

亡梦爱人 提交于 2019-11-27 07:01:34
问题 I would like to plot a time series using bar charts and have the Bin Width set to 0.9. I cannot seem to be able to do that however. I have searched around but could not find anything helpful so far. Is this a limitation if the stat="identity ? Here is a sample data and graph. Cheers ! time <- c('2015-06-08 00:59:00','2015-06-08 02:48:00','2015-06-08 06:43:00','2015-06-08 08:59:00','2015-06-08 10:59:00','2015-06-08 12:59:00','2015-06-08 14:58:00','2015-06-08 16:58:00','2015-06-08 18:59:00',

Grouped bar graph custom colours

谁说我不能喝 提交于 2019-11-27 06:31:01
问题 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

Put labels over negative and positive geom_bar

断了今生、忘了曾经 提交于 2019-11-27 06:14:02
问题 I'd like to visualize a data using bar chart and ggplot . I have the data below, when I want to vis this data the text for negative values are not showing below the Bar . dat <- read.table(text = "sample Types Value sample1 A -36 sample2 B 31 sample1 C 15 sample2 D -12 sample1 E 27 sample2 F 16 sample2 G -10 sample2 H 2 sample2 I 6 sample2 J -7 sample2 K -8" , header=TRUE) library(ggplot2) px <- ggplot(data = dat , aes(x = Types , y = Value , Colour = Types )) px + geom_bar(stat = "identity"

ggplot2: Setting geom_bar baseline to 1 instead of zero

北慕城南 提交于 2019-11-27 05:36:17
I'm trying to make a bar graph (with geom_bar) of ratios, and would like to set the x-axis at y=1. Therefore, ratios <1 would be below the axis and ratios >1 would be above the axis. I can do something similar with geom_point: ggplot(data, aes(x=ratio, y=reorder(place,ratio)))+geom_point()+geom_vline(xintercept=1.0)+coord_flip() However geom_bar would be much preferred... Ideally the graph would look something like this: http://i.stack.imgur.com/isdnw.png , except the "negative" bars would be ratios <1. Thanks so much for your help! C eipi10 You can shift the geom_bar baseline to 1 (instead of

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

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-27 04:39:41
问题 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

customize ggplot2 axis labels with different colors

橙三吉。 提交于 2019-11-27 04:21:58
问题 I have a basic bar graph I've created from ggplot2. The y variable contains both positive and negative values and about half the vector of values are negative. I would like to customize the axis labels such that when the y value of that corresponding x factor is a negative, its label is red. Here's a reproducible example: #Create data x <- c("a","b","c","d","e","f") y <- c("10", "9","-10","11","-3","-15") data <- data.frame(x, y) data$y <- as.numeric(as.character(data$y)) data$category <-

Order categorical data in a stacked bar plot with ggplot2

ぐ巨炮叔叔 提交于 2019-11-27 02:48:19
问题 I have a matrix with the following entries: dput(MilDis[1:200,]) structure(list(hhDomMil = c("HED", "ETB", "HED", "ETB", "PER", "BUM", "EXP", "TRA", "TRA", "PMA", "MAT", "MAT", "KON", "ETB", "PMA", "PMA", "HED", "BUM", "BUM", "HED", "PMA", "PMA", "HED", "TRA", "BUM", "EXP", "BUM", "PMA", "ETB", "MAT", "ETB", "ETB", "KON", "MAT", "TRA", "BUM", "BUM", "TRA", "TRA", "PMA", "PMA", "PMA", "MAT", "ETB", "TRA", "BUM", "TRA", "MAT", "BUM", "ETB", "TRA", "TRA", "BUM", "KON", "ETB", "ETB", "ETB", "BUM"

ggplot geom_bar: meaning of aes(group = 1)

妖精的绣舞 提交于 2019-11-27 00:37:02
问题 I am learning geom_bar on section 3.7 of r4ds.had.co.nz. I run a code like this: library(ggplot2) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1)) Then I have this plot: The point is, if I exclude the "group = 1" part: library(ggplot2) ggplot(data = diamonds) + geom_bar(mapping = aes(x = cut, y = ..prop..)) The plot will be wrong, But if I replace group = 1 by group = 2 or group = "x", the plot still looks correct. So I don't quite understand the meaning of

Generate paired stacked bar charts in ggplot (using position_dodge only on some variables)

*爱你&永不变心* 提交于 2019-11-26 23:04:46
I'm hoping to use ggplot2 to generate a set of stacked bars in pairs, much like this: With the following example data: df <- expand.grid(name = c("oak","birch","cedar"), sample = c("one","two"), type = c("sapling","adult","dead")) df$count <- sample(5:200, size = nrow(df), replace = T) I would want the x-axis to represent the name of the tree, with two bars per tree species: one bar for sample one and one bar for sample two. Then the colors of each bar should be determined by type. The following code generates the stacked bar with colors by type: ggplot(df, aes(x = name, y = count, fill = type