R ggplot2 facet_wrap isn't changing data according to Factor level

99封情书 提交于 2019-12-11 05:26:07

问题


I am trying to use facet_wrap to break up my data frame into individual plots based on one column. However, when I use levels, the title above each individual plot changes, but the data displayed in the chart does not.

Here's an example:

library(reshape2)

library(ggplot2)

levels(tips$sex) <- c("Male", "Female")
ggplot(tips, aes(x=total_bill, y=tip/total_bill))+
  facet_wrap(~sex)+
  geom_point(shape=1)

ggsave("prac.pdf")

This gives me a plot with Male on the left and Female on the right. The highest y data point is in the female graph.If I change the levels and have Female first, the Female label will appear on the left, but the highest y data point is now under male and still on the right.

levels(tips$sex) <- c("Male", "Female")
ggplot(tips, aes(x=total_bill, y=tip/total_bill))+
  facet_wrap(~sex)+
  geom_point(shape=1)

ggsave("prac.pdf") 

Any suggestions? I'm working with a different data frame of values, but the above example shows the same problem that I am having.

来源:https://stackoverflow.com/questions/54756565/r-ggplot2-facet-wrap-isnt-changing-data-according-to-factor-level

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!