This question already has answers here:
Bar plot with facets and same bar size (binwidth) with option to shrink the panel size
(1 answer)
ggplot2 different facet width for categorical x-axis [duplicate]
(1 answer)
ggplot2 facet_grid How to create different x-axis with keeping all values in each panel?
(1 answer)
Closed 12 months ago.
I am trying to recreate a bar graph (below) that has both stacked values (Yes/No/Not responding) and grouped values (e.g., the three bars under "Group activity" and the three bars under "In-cell activities").
I currently have a stacked graph using the following code
ggplot(f15dta,aes(x=number,y=factor(activity,level=rev(activities)),fill=fill,order=desc(fill)))+
geom_bar(stat="identity")+
guides(fill=guide_legend(reverse=T))
which produces the below stacked graph.
I have another variable in the data frame for the groups I want (e.g., "Group activity"). I tried using facets, but it looks like a complete mess (there are five groups/facets I want, but it graphs all the different bar graphs onto each one, so there's 90 bars instead of 18--see below)
Does anyone have any ideas on how to better approach stacked and grouped graphs in ggplot? I feel like such graphs have to be fairly common, but am unsure how to proceed. Normally I would group using fill and position=dodge, but since I am stacking with fill, I am slightly unsure what to do. Thanks for the help.
Related
This question already has answers here:
Plot with multiple lines in different colors using ggplot2
(1 answer)
ggplot combining two plots from different data.frames
(3 answers)
Multiple plots with variable color in R ggplot
(1 answer)
Closed 6 months ago.
I have 2 dataframes, "hot", and "cold" that I want to plot on the same ggplot scatterplot, in 2 different colours.
I want the "temperature" variable on the x-axis and "depth" variable on the y-axis.
I then want both dataframes plotted on this graph, with "hot" shown by red points and "cold" shown by blue points.
I have already plotted them individually, but can't figure out how to combine them into one plot. Example of the code used for the individual plots:
ggplot(hot, aes(x=temp, y=depth)) +
geom_point(size=0.1) +
labs(title="Depth Profile of Temperature in the Warm Months",x="Temperature (degC)", y = "Depth (m)") +
scale_y_reverse()
I have two bar charts with the same factor labels that I'd like to combine into one graph (lines 59-62) so that for each age group, there are two bars (one for perpetrator age and one for victim age) and each bar retains the stacked murdered/not murdered bar.
Any help appreciated. Thanks!
what you want is kind of impossible, because the bars should be stacked by two variables. There is for sure a way to do what you want, but the easiest way in my opinion is to reshape2::melt your data.frame and then facet side by side the two graphs.
m <- reshape2::melt(shootings[,c(2,5,8,9)],measure.vars=c("Perp_Age","Victim_Age"))
ggplot(m, aes(fill=Murdered, y=Cases, x=value))+
geom_bar(position='stack', stat='identity')+
facet_wrap(~variable)
wich gives you this plot:
This question already has an answer here:
Reversed order of bars in grouped barplot with coord_flip
(1 answer)
Closed 2 years ago.
From spss there is a kind of clustering which is called two step cluster.
The vizual option is provided by spss is something like this left side plot.
Having the results of clusters, label/names of the variables used and their score into a dataframe like this
data.frame(cluster = c(1,1,1,2,2,2,3,3,3), value = c("Google","Amazon","Yahoo","Google","Amazon","Yahoo","Google","Amazon","Yahoo"), score = c(2194.2,43.2,4331.3,31.3,133.1,432.1,3234.1,44.3,21.4))
These are the inputs as refered in the spss plot.
is there any efficient way to vizualize them using ggplot2?
Maybe something like this:
library(ggplot2)
#Plot
ggplot(df,aes(x=cluster,y=score,fill=value))+geom_bar(stat='identity',position = 'stack')+
coord_flip()
This question already has answers here:
Plotting two variables as lines using ggplot2 on the same graph
(5 answers)
Closed 3 years ago.
I am trying to plot multiple line on one chart.
They all have the same X- axis in months, but different observation for y-axis.
I have tried writing this code but I keep on getting an error.
Can someone point me towards what I am doing wrong?
"Test3" is the same of my data set, "Oil_1" represents the first Y observation, "Oil_2" second observation and "Month" is the X-axis
ggplot(test3, + aes(x = Months)) +
geom_line(aes(y=oil_1),colour="blue")+
geom_line(aes(y=oil_2),colour="red") +
ylab(label="Production")+
xlab("Months")
You have an extra "+" before the first aesthetic call, that could be the problem.
This question already has answers here:
Order Bars in ggplot2 bar graph
(16 answers)
Closed 6 years ago.
I have a data frame df1 and want to draw a barplot of AccountExecutive and their corresponding ClearRate where the bars are arranged so that it is decreasing from left to right.
I tried this code but the resulting graph still reflects AccountExecutive order as it appears in df1
ggplot(arrange(df1, -ClearRate), aes(x = AccountExecutive, y = ClearRate)) +
geom_bar(stat="identity")
Can anyone help me correcting this code?
NOTE: Not a duplicate of the previous question because that one asks for an arbitrary positioning of the x axis labels. This question asks how to sort x-axis labels considering their y-axis values.
Try this one the code below should reorder AE according to clearance rate
ggplot(df1,aes(x=reorder(AccountExecutive,-ClearRate),y=ClearRate))+geom_bar(stat"identity")
here is the more about reorder function
Reorder bars in geom_bar ggplot2