geom-bar

colour bins of histogram in R

╄→尐↘猪︶ㄣ 提交于 2019-12-11 16:49:47
问题 I want to plot a barplot like in figure. I want each bin to be coloured based on the sum of the value in the other columns. . I made a reproducible example here. library(reshape) library(ggplot2) values= replicate(4, diff(c(0, sort(runif(92)), 1))) colnames(values) = c("A","B","C","D") counts = sample(10:100, 93, replace=T) df = data.frame(cbind(values,"count"=counts)) mdf = melt(df,id="count") mdf = mdf %>% mutate(binCounts = cut(count, breaks = seq(0, 100, by = 5))) plot = ggplot(mdf) +

format internal lines of a stacked geom_bar ggplot

僤鯓⒐⒋嵵緔 提交于 2019-12-11 16:31:12
问题 I want to remove the internal borders from my ggplot, leaving a coloured border around the outside of each bar only. Here is a test data frame, with a stacked bar plot. Ideally, I will end up with the groups in the stack still being a shade of grey, with a colourful outline per box. test <- data.frame(iso=rep(letters[1:5],3), num= sample(1:99, 15, replace=T), fish=rep(c("pelagic", "reef", "benthic"), each=5), colour=rep(rainbow(n=5),3)) ggplot(data=test, aes(x=iso, y=num, fill=fish, colour

R ggplot2: Plot percentage values on top of Bar chart with proportion calculated within the geom_bar function

喜欢而已 提交于 2019-12-11 15:25:25
问题 Data used : structure(list(Year = c(2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L, 2014L), Rating = c(4L, 4L, 4L, 3L, 4L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 4L, 5L, 5L,

Show legend for bar plot in ggplot

醉酒当歌 提交于 2019-12-11 11:48:16
问题 How do I show legend for the following ggplot bar plot? tmp <- data.frame(group = LETTERS[1:10], id = 10:1, a = runif(10), b = runif(10)) ggplot(tmp) + geom_bar(aes(x = reorder(group, id), a + b, group = 1), stat = 'identity') Update: I have two charts arranged using grid.arrange from gridExtra . Both charts have the same number of bars, but one has legend. I thought that by adding any legend to the second chart, I will align the bars (make width of plot area of both plots the same): tmp <-

Plot hline at mean with geom_bar and stat=“identity”

杀马特。学长 韩版系。学妹 提交于 2019-12-11 05:37:45
问题 I have a barplot where the exact bar heights are in the dataframe. df <- data.frame(x=LETTERS[1:6], y=c(1:6, 1:6 + 1), g=rep(x = c("a", "b"), each=6)) ggplot(df, aes(x=x, y=y, fill=g, group=g)) + geom_bar(stat="identity", position="dodge") Now I want to add two hlines displaying the mean of all bars per group. All I get with ggplot(df, aes(x=x, y=y, fill=g, group=g)) + geom_bar(stat="identity", position="dodge") + stat_summary(fun.y=mean, aes(yintercept=..y.., group=g), geom="hline") is As I

How to change the space between bars in geom_bar?

廉价感情. 提交于 2019-12-11 04:04:41
问题 I have a bar plot with positive values on both sides. When I changed the width of the bars, the space between them became to large and doesn't look good. I tried to manipulate this with position = position_dodge , but it doesn't work. How can I reduce the spaces between the bars? Here is the code (originally posted here Stacked barplot crossing the x-axis) with my data: Year <- factor(c("2003-2009","2003-2009","2003-2009","2003-2009","2003-2009","2009-2012", "2009-2012","2009-2012","2009-2012

Moving table created by annotation_custom with geom_bar plot

♀尐吖头ヾ 提交于 2019-12-11 03:21:59
问题 I tried searching for answers but couldn't find anything. I have have a plot and want to add a table within the plot itself. I can do it but the table ends up being right in the middle. It is possible to relocate a table created by annotation_custom if the x-axis is discrete? If so, how? Thank you! For example, I want to relocate this table. library(ggplot2) library(gridExtra) my.summary <- summary(chickwts$weight) my.table <- data.frame(ids = names(my.summary), nums = as.numeric(my.summary))

Automatically resize bars in ggplot for uniformity across several graphs R

时间秒杀一切 提交于 2019-12-10 20:43:23
问题 I generate several bars graphs in a loop, and they all resize according to the output size (assume from plot/device size?) rather than according to the bar size. This means that plots with two bars have fat bars, and plots with, say, 6 bars, have thin bars; both outputs are the same size though. The code below represents my script with reproduceable data (I do many other aes/theme changes to mine). I'd like the output plot to resize (in the dimension of bar width) so that the bars are always

How to increase the space between grouped bars in ggplot2?

本小妞迷上赌 提交于 2019-12-10 14:23:36
问题 [Data and code to produce plot at the end of the post] Using ggplot, I plotted a bargraph with error bars, bars are grouped by two factors (one on X axis, one with fill). I would like to increase the green distance between the groups on the xaxis to make the plot easier to read: The closest thing to a solution on stackoverflow I found here (where someone asked my question in an unanswered comment), here, here, but I didn't manage to apply these without massing up the error bars. Can someone

manipulate delineation of geom_bar and coord_polar in ggplot2 r

对着背影说爱祢 提交于 2019-12-10 13:37:42
问题 I am building a chart of concentric circles using polar_coord in ggplot and I need to get rid of a specific line. Here's the code and the plot: df <- data.frame(A=letters[1:12], B=c(rep("Dim_1",4),rep("Dim_2",4),rep("Dim_3",4)), C=c(rep("Ind_1",2),rep("Ind_2",2),rep("Ind_3",2),rep("Ind_2",2),rep("Ind_5",2),rep("Ind_6",2))) ggplot(df,aes(factor(1),fill=C))+ geom_bar(width = 1,colour="black")+ coord_polar()+ scale_fill_manual(values = c("#FFFFFF","#CCCCCC","#CCCCCC","#999999","#999999")) How do