使用ggplot 2创建{

时间:2018-09-08 22:39:06

标签: r ggplot2 likert

我有一个结构如下的数据框

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")

我想绘制一个李克特量表,如下图所示 Expected Likert image output

我正在努力在ggplot 2中复制它(下面是我的代码,即使它提供的输出与预期的相似)y轴上的空格也很远,因为我在x和y参数中都使用了Genderity。如果我将y参数留空

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)

从我的代码输出ggplot My Output

我知道我的输出确实接近我想要的输出,但它不是正确的输出方式。请帮助通过ggplot 2做到这一点

2 个答案:

答案 0 :(得分:3)

这似乎是将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

答案 1 :(得分:1)

我们可以尝试解决方法:

# 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

而且,如果您想在模型中订购该订单,并且还希望x轴空白,可以尝试以下操作:

# 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

并且:

+ ggtitle("Conveyed gender")

会给你标题。

编辑:
要添加标签,您必须:

    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

或者您也可以使用:

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