So I'm trying to use geom_bar in ggplot2, and all of the cases that I see of people demonstrating it online are of comparative frequencies of certain things. The chart that I'm trying to do is the stacked bar graph like this one
However, I want to do it from a vector of values. That is, let's say I have the vector
v=c(1,2,3,4)
Instead of 4 even bars, which is what I understand I would get, I'd like a stack of 4 bars where the top one is 1 unit tall, and the next one down is 2 units tall (etc.). Is this possible in R?
Edit: Here is the code that I've used for my graph. It's yielding a normal bar graph, not the stacked version that I'm looking for:
ggplot(data = v, aes(x = factor(x), y = y)) + geom_bar(aes(fill = factor(y)),stat = 'identity')
I think you can start from this:
v=data.frame(x="My Stacked Bar", y=c(1,2,3,4))
ggplot(data = v, aes(x = factor(x), y = y))+
geom_bar(aes(fill=factor(y)), stat="identity")
Related
Hi everyone I want to do a grid bar plot of my data which is the relative abundance of fungi and make it as neat as possible but when I use facet grid the bar plots don't look right to facilitate comparison. Here is the data:
When I use the code :
theme_set(theme_bw())
facet_plot <- ggplot(my_data, aes(x = depth, y = relative abundance, fill = Treatment)) +
geom_bar(stat = "identity", position = "dodge") +
geom_errorbar(aes(ymin=relative abundance-se, ymax=relative abundance+se), width=.2,position=position_dodge(.9))+
facet_grid(. ~ phylum)
I get a plot that looks like this:
As you can see , the plot looks strange especially the last three barplots. Does anyone know how I can modify my code so each plot has its own y axis or any other way of adjusting the scale?
Best wishes
I am trying to create a stacked bar chart and since some of my values are very small, instead of placing the labels inside each bar. I was wondering if I can place each values on top of the bar with the highest value and stacked below it. Then the text colour will be respective with the bar colour. I tried y=val+1 in the geom_text(), however, that would add the highest value as well. Which is not what I want.
x<-c(1,2,3,4,5)
y1<-c(0.5,4,2,9,16)
y2<-c(.25,3,10,0.02,7)
y3<-c(2,2,16,0.023,4.5)
df1<-data.frame(x=x,v1=y1,stringsAsFactors=FALSE)
df2<-data.frame(x=x,v2=y2,stringsAsFactors=FALSE)
df3<-data.frame(x=x,v3=y3,stringsAsFactors=FALSE)
pClass<-left_join(df1,df2,by="x")
pClass<-left_join(pClass,df3,by="x")
pClass<-pClass%>%pivot_longer(-x,names_to="var",values_to="val")%>%mutate(val=as.numeric(val))
ggplot(pClass,aes(x=x,y=val,fill=var))+geom_bar(position="stack",stat="identity")+
geom_text(aes(x=x,label = prettyNum(as.numeric(val),digits = 3),y=val),size = 3,fontface="bold")
Roughly I would want something like this: Each of the bars I will have 3 values sitting on top of the bars.
It will be tough to show some of the labels in the small categories, but this should do it:
ggplot(pClass,aes(x=x,y=val,fill=var, label=prettyNum(as.numeric(val),digits = 3)))+
geom_bar(stat="identity")+
geom_text(position="stack")
Or given the small intervals for your data, don't stack the bar plot:
ggplot(Data, aes(x = Year, y = Frequency, fill = Category)) +
geom_bar(stat="identity", position=position_dodge())+
geom_text(aes(label=Frequency), position=position_dodge(0.9), vjust=0)
This is should be very simple question!
I would like to make a barplot with errorbars and I'm using the following code:
ggplot(data = bars, aes(x=c("1","2","3"), y=V2, fill = names)) +
geom_bar(position=position_dodge(), stat="identity", alpha = 0.7) +
geom_errorbar(aes(ymin=V1, ymax=V3))+
theme(legend.position='none')+
coord_cartesian(ylim=c(0,10))
However, I have 2 problems:
1. I would like the bars to start at y = 0
2. I don't like the ticks in the y axis. I would like numbers with just one decimal and less ticks.
this is my actual plot: Bars with error bars
For the first problem (if I understand it correctly) you can use ylim
... + ylim(0.2, NA)
NA leaves the upper bound free.
For the second, I suggest to use pretty_breaks from scale
library(scales)
... + scale_y_continuous(breaks=pretty_breaks(n=5))
Maybe it's because of the dark outside, but I can't get this
Position geom_text on dodged barplot
to work on my fairly simple dataframe
fs <- data.frame(productcategory=c("c2","c2"), product=c("p4", "p5"), ms1=c(2,1))
plot <- ggplot(data=NULL)
plot +
geom_bar(data=fs, aes(x=productcategory, y=ms1, weight=ms1, fill=product),stat="identity", position="dodge") +
geom_text(data=fs, aes(label = ms1, x = productcategory, y=ms1+0.2), position=position_dodge(width=1)))
My plot still shows the labels in the "middle" of the product category and not above of the proper product.
Looks like this even it seems very simple, but I'm totally stuck on this
So any hints are very much appreciated how to get labels above the proper bars.
Tom
Because you have the aesthetics defined for each geom individually, geom_text isn't picking up on the fact that you're subdividing the x variable productcategory by the fill variable product.
You can get the graph you want by adding fill=product to the aes() call for geom_text, or you can try to define as many aesthetics as possible in the original ggplot() call, so that all the geoms pick up on those aesthetics automatically and you only have to define them if they're specific to that particular geom.
plot2 <- ggplot(data=fs, aes(x=productcategory, y=ms1, fill=product)) +
geom_bar(stat="identity", position="dodge") +
geom_text(aes(label=ms1, y =ms1 + 0.2), position=position_dodge(width=1))
print(plot2)
I have a data frame with (to simplify) judges, movies, and ratings (ratings are on a 1 star to 5 star scale):
d = data.frame(judge=c("alice","bob","alice"), movie=c("toy story", "inception", "inception"), rating=c(1,3,5))
I want to create a bar chart where the x-axis is the number of stars and the height of each bar is the number of ratings with that star.
If I do
ggplot(d, aes(rating)) + geom_bar()
this works fine, except that the bars aren't centered over each rating and the width of each bar isn't ideal.
If I do
ggplot(d, aes(factor(rating))) + geom_bar()
the order of the number of stars gets messed up on the x-axis. (On my Mac, at least; for some reason, the default ordering works on a Windows machine.) Here's what it looks like:
I tried
ggplot(d, aes(factor(rating, ordered=T, levels=-3:3))) + geom_bar()
but this doesn't seem to help.
How can I get my bar chart to look like the above picture, but with the correct ordering on the x-axis?
I'm not sure your sample data frame is representative of the images you put up. You mentioned your ratings are on a 1-5 scale, but your images show a -3 to 3 scale. With that said, I think this should get you going in the right direction:
Sample data:
d = data.frame(judge=sample(c("alice","bob","tony"), 100, replace = TRUE)
, movie=sample(c("toy story", "inception", "a league of their own"), 100, replace = TRUE)
, rating = sample(1:5, 100, replace = TRUE))
You were closest with this:
ggplot(d, aes(rating)) + geom_bar()
and by adjusting the default binwidth in geom_bar we can make the bar widths more appropriate and treating rating as a factor centers them over the label:
ggplot(d, aes(x = factor(rating))) + geom_bar(binwidth = 1)
If you wanted to incorporate one of the other variables in the chart such as the movie, you can use fill:
ggplot(d, aes(x = factor(rating), fill = factor(movie))) + geom_bar(binwidth = 1)
It may make more sense to put the movies on the x axis and fill with the rating if you have a small number of movies to compare:
ggplot(d, aes(x = factor(movie), fill = factor(rating))) + geom_bar(binwidth = 1)
If this doesn't get you on your way, put up a more representative example of your dataset. I wasn't able to recreate the ordering problems, but that could be due to a difference in the sample data you posted and the data you are analyzing.
The ggplot website is also a great reference: http://had.co.nz/ggplot2/geom_bar.html