Reputation: 51
I want to essentially recreate this plot that I made in JMP but with R. The values come from a qualitative dataset, where the species groups are entries in a single column. I would like to plot the SpeciesGroup on the x axis, but fill the color in by the Strength, in a stacked bar. I think I need to reorder the data, but can't quite figure out how.
df<- data.frame(ID=c(1,2),
policy=c("Policy A", "Policy B", "Policy C", "Policy D",
"Policy E","Policy F" ),
SpeciesGroup= c("ray", "ray", "mammal", "mammal", "fish", "reptile"),
Stength=c("M", "V", "M", "P", "P", "M"),
stringsAsFactors=FALSE)
So far I have tried:
ggplot(aes(x=factor(Group), fill = Regulatory_Strength)) +
geom_bar(mapping = NULL, data = RFMO_Policies, stat = "count",
position = "stack", width = .7, binwidth = NULL,
na.rm = FALSE, show.legend = TRUE, inherit.aes = TRUE )+
labs(title="Policy Directives by Taxa", x="Directive Type", y = "Count", size = 13 )+
theme(plot.title = element_text(hjust=1))+
theme_classic()+
scale_fill_manual(values=c( "firebrick3","yellow1","dodgerblue1" ),
labels= c("Mandatory","Partial", "Voluntary" ))+
theme(axis.text.x=element_text(angle=45,hjust=1,vjust=1, size= 13))+
guides(fill=guide_legend(title="Regulatory Strength"))
Below is the graph I made in JMP that I would like to reproduce:
Thank you for any help you can offer!!
Upvotes: 1
Views: 283
Reputation: 16178
Is it what you are looking for ?
library(ggplot2)
ggplot(df, aes(x = SpeciesGroup, fill = Stength))+
geom_bar(stat = "count")
Alternative using geom_tile
To my opinion, as your data are qualitative values, maybe it will make more sense to plot them using geom_tile
:
library(dplyr)
All_val <- expand(df,SpeciesGroup, Stength)
library(ggplot2)
df %>% mutate(Val = Stength) %>%
select(SpeciesGroup, Stength, Val) %>%
full_join(., All_val) %>%
ggplot(aes(x = SpeciesGroup, y = Stength, fill = Val))+
geom_tile(color = "black")+
scale_fill_discrete(na.value = "white")+
geom_text(aes(label = Val))
Upvotes: 2