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.
This question already has an answer here:
ggplot2 geom_bar - how to keep order of data.frame [duplicate]
(1 answer)
Closed 2 years ago.
I want to change the order of the stacked bar plot in R? I am sharing the example below and the green bar will be under the blue bar, how can I can this order? See the code below.
d1<-data.frame(Gene=c("DNA","DNA",
"RNA","RNA",
"XX","XX"),
Gender=c("M","F","M","F","M","F"),
p_value=c( 0.5, 0.1,
0.6,0.01,
0.07,0.02
))
p<-d1 %>%
ggplot(aes(x=forcats::fct_reorder(Gene,p_value), y=p_value, fill=Gender)) +
geom_col(color="black",position=position_dodge()) +
coord_flip() +
scale_fill_manual(values=c('#6495ED','#2E8B57'))+
labs(x="Gene", y="p-value")
Just convert d1$Gendered into a factor and specify the levels in the order you want them.
d1$Gender <- factor(d1$Gender, levels = c("M", "F"))
Then, run the code to create your 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:
A way to always dodge a histogram? [duplicate]
(2 answers)
Closed 8 years ago.
In this example:
library(ggplot2)
dat <- data.frame(a=factor(c(1,1,1,2,2,2,3,3,3,4)), b=c("A","B","D","A","B","C","A","B","D",NA), c=c(1,4,3,5,5,1,2,2,8,6))
plot <- ggplot(dat,aes(fill=b,x=a,y=c))
plot + geom_bar(width=.7, position=position_dodge(width=.7), stat = "identity")
factor 4 is wider than the other bars. Is there a way to make them all the same width?
Ideally you should have data for every combination even if it is zero. That means, with 1 in data$a you should have data all the four(A,B,C,D) and so on... try modifying your data frame like this and plot. NA category was referred to as "other" here.
library(ggplot2)
dat <- data.frame(a=factor(c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4)),
b=c("A","B","C","D","other","A","B","C","D","other","A","B","C","D","other","A","B","C","D","other"),
c=c(1,4,0,3,0,5,5,1,0,0,2,2,0,8,0,0,0,0,0,6))
plot <- ggplot(dat,aes(fill=b,x=a,y=c))
plot + geom_bar(width=.7, position=position_dodge(width=.7), stat = "identity")
View this dataframe you will know the difference. You will obviously have missing bars corresponding to your data, which dnt look good. But im afraid this might be the only solution.