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)
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 made a stacked barplot in ggplot2 in R:
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill")+ scale_y_continuous(name = "Y-axis",labels = scales::percent)
I want to just visualize the top portion of the stacked barplot like so:
I've looked everywhere and can't figure out how to do this. Does anyone know how?
You can use coord_cartesian to "zoom in" on the area you want.
# your plot code...
ggplot(Count_dataframe_melt, aes(x = as.factor(variable), y = value, fill = fill)) +
geom_bar(stat = "identity",position="fill") +
scale_y_continuous(name = "Y-axis",labels = scales::percent) +
# set axis limits in coord_cartesian
coord_cartesian(ylim = c(0.75, 1))
Note that many people consider bar plots that don't start at 0 misleading. A line plot may be a better way to visualize this data.
Since the areas you want to show are less than 20% of the total area, you could flip the bar charts so that you only show the colors areas. Then the y-axis goes from 0-25% and you can use the figure caption to describe that the remaining data is in the gray category.
Probably a simple ggplot2 question.
I have a data.frame with a numeric value, a categorical (factor) value, and a character value:
library(dplyr)
set.seed(1)
df <- data.frame(log10.p.value=c(-2.5,-2.5,-2.5,-2.39,-2,-1.85,-1.6,-1.3,-1.3,-1),
direction=sample(c("up","down"),10,replace = T),
label=paste0("label",1:10),stringsAsFactors = F) %>% dplyr::arrange(log10.p.value)
df$direction <- factor(df$direction,levels=c("up","down"))
I want to plot these data as a barplot using geom_bar, where the bars are horizontal and their lengths are determined by df$log10.p.value, their color by df$direction, and the y-axis tick labels are df$label, where the bars are vertically ordered by df$log10.p.value.
As you can see df$log10.p.value are not unique, hence:
ggplot(df,aes(log10.p.value))+geom_bar(aes(fill=direction))+theme_minimal()+coord_flip()+ylab("log10(p-value)")+xlab("")
Gives me:
How do I:
Make the bars not overlap each other.
Have the same width.
Be separated by a small margin?
Have the y-axis tick labels be df$label?
Thanks
Here is one possible solution. Please note that, by default, geom_bar determines the bar length using frequency/count. So, you need to specify stat = "identity" for value mapping.
# since all of your values are negative the graph is on the left side
ggplot(df, aes(x = label, y = log10.p.value, fill = direction)) +
geom_bar(stat = "identity") +
theme_minimal() +
coord_flip() +
ylab("log10(p-value)") +
xlab("")
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")
I run the following code to get a stacked bar chart of my data
qplot(factor(course.name):factor(condition):factor(condition.player),data=dfAll,geom="bar",fill=factor(textbook.usage.distribution))
I want to scale each bar to the same hight, but keep the proportion of the stacks the same. It should become something like this :
How can I achieve that? I tried
+ geom_bar(aes(y = (..count..)/sum(..count..)), binwidth = 25) +
scale_y_continuous(labels = percent_format())
But it does not scale the bars to the same height, and the percentage seems to be based on the total number of A+B+C+D of all observations.