条形图 - 在负轴上创建正标签 - scale_y_continuous()不起作用

时间:2018-06-14 20:19:45

标签: r ggplot2 bar-chart

我想制作一个工人情节'工资等级并比较基于办公室的人在家的人。

我想要一组条形图(彩色)代表我数据框中基于家庭的人,而另一组条形图(黑色)代表基于办公室的人。

问题在于我希望情节的方向与ggplots()的默认情况相反(参见下面所需图表的绘图)。当我尝试使用scale_y_continuous()我的代码中断来解决标记问题时(我使用scale_y_continuous()因为我首先使用了coord_flip())。

任何帮助将不胜感激!

enter image description here

# Data 
df <- data.frame(question = c("work", "work", "work", "work", "work", "work"),
                 location = c("homebased", "homebased", "homebased", "officebased", "officebased", "officebased"),
                 salary = c("grade 1/2", "grade 3", "grade 4/5", " grade 1/2", "grade 3", "grade 4/5"),
                 freq = c(41.3, 53.5, 5.2, 37.2, 54.3, 9.5))

# Prep data to change direction of default plot
df$freq <- df$freq*-1

# Plot
library(ggplot2)
ggplot(mapping = aes(x = question, y = freq, fill = factor(salary))) +
  geom_col(data = df[df$location == "homebased", ], position = "dodge") +
  geom_col(data = df[df$location == "officebased", ], aes(group = salary),
           fill = "black", width = 0.45, position = position_dodge(width = 0.9))  + 
  coord_flip () +  theme_classic() +
  scale_x_discrete(name = "", position = "top") +      
  scale_y_continuous("freq", limits = c(0, -60), breaks =c(-60, -40, -20, 0),
                     labels = c(60, 40, 20, 0))

0 个答案:

没有答案