Create Likert scale sort of graph using ggplot 2

Create Likert scale sort of graph using ggplot 2



I have a dataframe with below structure


structure(list(Set = structure(c(1L, 1L, 1L, 2L, 2L, 2L), .Label = c("Set2",
"Set1"), class = "factor"), Subset = c("Feminine", "Masculine",
"Neutral", "Feminine", "Masculine", "Neutral"), Genderity = c(4.47,
-3.65, 1.54, 4.13, -4.03, -0.61)), row.names = c(NA, -6L), class = "data.frame")



I want to plot a likert scale sort of plot as below output
Expected Likert image output



I am struggling to replicate this in ggplot 2 (below is my code, even though it gives similar output as expected The spaces in y axis are way off because I used Genderity in both x & y argument). It was giving me error if I left y argument blank


ggplot(aes(x=Genderity, y=Genderity), data=df_2, position="stack", stat="identity")+
geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
coord_flip()+
geom_hline(yintercept = 0, color =c("black")) +
scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
facet_wrap(facets = .~Set, ncol = 2, nrow=1)



Output ggplot from my code
My Output



I know my output is really close to what I want but its not the right way to do it. Please help how to do this via ggplot 2




2 Answers
2



We can try a workaround:


# add a fake column
df_2$fake <-rownames(df_2)

library(ggplot2)

# we add as x the fake
ggplot(aes(x=fake, y=Genderity), data=df_2, position="stack")+
geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
coord_flip()+
geom_hline(yintercept = 0, color =c("black")) +
scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
# to avoid riffle, we use the scales="free" option
facet_wrap(facets = .~Set, ncol = 2, nrow=1,scales="free")+
# last, we make blank the y axis
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank())



enter image description here



And, if you want the order in your mock-up and if you want also the x-axis blank, you can try this:


# the reorder option:
ggplot(aes(x=reorder(fake,Genderity), y=Genderity), data=df_2, position="stack")+
geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
coord_flip()+
geom_hline(yintercept = 0, color =c("black")) +
scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
facet_wrap(facets = .~Set, ncol = 2, nrow=1,scales="free")+
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
# x-axis blank:
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())



enter image description here



And:


+ ggtitle("Conveyed gender")



Will give you the title.



Edit:


to add labels, you've to:


p <- ggplot(aes(x=reorder(fake,Genderity), y=Genderity), data=df_2, position="stack")+
geom_bar(stat="identity",aes(fill=Subset),position="dodge")+
coord_flip()+
geom_hline(yintercept = 0, color =c("black")) +
scale_y_continuous(breaks=seq(-5,5,1), limits=c(-5,5))+
facet_wrap(facets = .~Set, ncol = 2, nrow=1,scales="free")+
theme(axis.title.y=element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
# x-axis blank:
axis.title.x=element_blank(),
axis.text.x=element_blank(),
axis.ticks.x=element_blank())
p <- p + ggtitle("Conveyed gender") + geom_text(aes(label=Genderity), hjust=1)
p



enter image description here



Or you can use also:


+ geom_text(aes(label = Genderity), position = position_dodge(0.9))






Can you please show me how to add labels next to bars as well

– Artika
Sep 10 '18 at 9:49






@Artika updated the answer, it should work also with the other answer.

– s_t
Sep 10 '18 at 10:08






HI In this my label is getting blocked as the color of my neutral is black and text color is also. I can't see it as it is falling inside the graph

– Artika
Sep 10 '18 at 11:18






@Artika I've updated with the ouput given by my code. Sorry, I did not understood what you'd like to modify, if you need other explaination, please ask me again in other word.

– s_t
Sep 10 '18 at 11:28






The value 1.54,4.47,4.13 which are falling inside the bar, I want them to be like -0.61 just outside the bar. So basically I want all the values to in position which green bars have just on top of bar

– Artika
Sep 10 '18 at 11:44



This seems like a good place to convert your Subset character string into a factor, which will let you sort it as you wish. Note, in defining the mapping, I used fct_rev to get the Feminine bar to be on top, to line up with the legend, which shows the factors in order.


Subset


fct_rev


df_2_mod <- df_2 %>%
mutate(Subset = fct_relevel(Subset, "Feminine", "Neutral", "Masculine"))

ggplot(df_2_mod,
aes(x=Subset %>% fct_rev(), y=Genderity, fill = Subset))+
geom_col()+ # geom_col uses values by default, vs. counts for geom_bar
coord_flip()+
geom_hline(yintercept = 0, color =c("black")) +
facet_wrap(facets = .~Set, nrow=1) +
labs(x="")



enter image description here



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)