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.
Related
Currently i have plotted a stacked bargraph to display the amount of people having delays in each month with the following code
q1%>%
mutate(DepDelay1 =factor(x=DepDelay1, levels=c(0,1), labels=c("No","Yes"))) %>%
filter(Year==2005)%>%
ggplot(aes(x=factor(Month), fill=DepDelay1)) +
geom_bar(position="stack") +
ggtitle("DepDelay of Flight") +
guides(fill=guide_legend("Delay")) +
xlab ("Month")+
ylab ("Flight Count")+
geom_text(aes(label=..count..),
stat='count',
colour = "white",
size = 2.5,
position = position_stack(vjust = 0.5))
Graph Example for position="stack"
However i cannot seem to produce a 100% stacked bargraph after changing from
geom_bar(position="stack")
to
geom_bar(position="fill")
Graph Example for position="fill"
This method works before i adjust the geom_text and adding the filter to my current code.
Anyone know what's the issue ? From what i understand, a Y-variable is needed however i don't have a suitable value to input it from the dataset and i'm trying to plot the count per month.
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)
I have created a mixed plot (2 lines and 1 bar plot) using GGPlot. The bars represent the percent difference between the two lines on the plot. It looks good except for the positioning of the bars relative to the lines. I would like to be able to adjust the secondary y-axis (i.e. the bar plot axis) to range from -50 to 50 (i.e. centre at 0) without affecting the primary y-axis, which I would like to have start at 0. Does anyone know a way to do this? The current code that I am working on and the resulting plot look like this:
year<-c(1995:2018)
value1<-c(1116.87,3030.85,2676.40,3809.81,2459.74,2666.61,2678.28,3303.45,1839.21,3567.79,4529.22,5838.21,7762.13,8079.70,9615.55,9645.06,8297.23,8974.69,12757.06,13052.86,13670.74,17598.57,17190.01,20192.92)
value2<-c(998.22,3551.52,2421.50,3647.22,2085.44,2558.46,2863.98,3332.18,1606.40,3445.12,4893.11,5486.48,7242.37,7356.78,8810.64,7787.83,7507.25,8442.26,10347.11,11002.82,8783.90,15604.60,14648.09,15368.58)
df1<-data.frame(year,value1,value2)
df1$diff<-((df1$value1-df1$value2)/df1$value1)*100
library(ggplot2)
plot1<-ggplot(df1, aes(x = year)) +
geom_bar(aes(x=year, y = (diff*200), fill="percent diff"), stat="identity") +
geom_line(aes(y=value1,color="A")) +
geom_line(aes(y=value2, color="B")) +
scale_y_continuous(sec.axis = sec_axis(~./200, name = "percent diff")) +
ylab("Value") +
theme(panel.grid.minor = element_blank(),legend.title = element_blank(), legend.spacing.y = unit(-0.1, "cm")) +
scale_color_manual(" ",values=c("A"="#619CFF", "B"="#F8766D", "percent diff"=NA)) +
scale_fill_manual(" ",values=c("percent diff"="gray"))
plot1
To be clear, the below plot (done in Excel) is what I am trying to accomplish.
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.
ggplot(G, aes(x=State, y=Score, fill=State))+
geom_bar(stat="identity", position="dodge")+
scale_y_continuous(labels = scales::comma)
Please help me make more elegant to read.
this output
+ I wanna use a line in x at the score of 236, tried
abline( v=236)
did not work!
Try this, it works for me
barplot(c(1,2,3,4),space=c(1,1,1,1)) # equally spaced bars as expected
barplot(c(1,2,3,4),space=c(1,20,1,1)) # massive gap before the 2nd bar
barplot(c(1,2,3,4),space=c(20,1,1,1)) # the same as the first plot
That's a lot of bars. You can make the bars narrower by specifying their width inside geom_bar() (as a proportion, 1 is touching, 0.5 is equal amounts of bar and gap, the default is 0.9).
ggplot(G, aes(x = State, y = Score, fill = State)) +
geom_bar(stat = "identity", position = "dodge", width = 0.8) +
scale_y_continuous(labels = scales::comma)
Also note that the position = "dodge" isn't doing anything in your example.
For a plot with that many bars, if you want them all labeled, I would suggest adding + coord_flip() to your plot - usually it's easier to have lots of vertical space than lots of horizontal space, and the long labels won't overlap. When you have over 50 bars, you're going to need a fair amount of space.