如何在ggplot2中正确使用facet_grid? [英] How to use facet_grid correctly in ggplot2?

查看:391
本文介绍了如何在ggplot2中正确使用facet_grid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用下面的代码为每个配置文件生成一个图表,但我一直得到至少有一个图层必须包含用于构建切面的所有变量。错误。我花了最后几个小时试图让它工作,但我不能。

我相信anwser必须很简单,任何人都可以帮忙吗?

  d = structure(list(category = structure(c(2L,2L,1L,1L,1L,1L,
1L,1L, 1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L,1L, 1L,1L,1L,3L,3L,3L,3L,
3L,1L,1L,1L,1L,1L,1L,1L,1L,2L,2L,2L),标签= c(4X4 (1L,
1L,1L,1L,3L,3L,3L,3L,3L,3L,3L,3L,3L, 3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L,3L, 3L,3L,3L,3L,3L,3L,3L,3L,2L,2L,2L,2L,2L,2L,2L,1L,1L,
1L),标签= c (6440.32,6287.22,9324,7532,7286.63,6827.27,
6880.48,7795.15,7042.51,
值= c(固定,免费,移动), 2708.41,1373.69,6742.87,7692.65,
7692.65,8116.56,7692.65,7692.65,7692.65,7962.65, 8116.56,
5691.12,2434,83​​43,7727.73,7692.65,7721.15,1944.38,
6044.23,8633.65,7692.65,7692.65,8151.65,7692.65,7692.65,
2708.41,3271.45,3333.82,1257.48, 6223.13,7692.65,6955.46,
7115.46,7115.46,7115.46,7115.46,6955.46,7615.46,2621.21,
2621.21,445.61)),.Name = c(category,profile,value
),class =data.frame,row.names = c(NA,-50L))

library(ggplot2)

p = ggplot(d ,aes(x = d $ value,fill = d $ category))+ geom_density(alpha = .3)
p + facet_grid(d $ profile〜。)
pre>

解决方案

你的问题来自于显式引用变量(即 d $ profile ),而不是针对 ggplot 中的 data / code>。在任何地方都不需要 d $



faceting 使用 facet_grid facet_wrap ,您需要这样做。调用 aes

  p = ggplot(d,aes(x = value,fill = category))+ geom_density(alpha = .3)
p + facet_grid(profile〜。)


I'm trying to generate one chart per profile with the following code, but I keep getting "At least one layer must contain all variables used for facetting." errors. I spent the last few hours trying to make it work but I couldn't.

I believe the anwser must be simple, can anyone help?

d = structure(list(category = structure(c(2L, 2L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 3L, 3L, 3L, 3L, 
3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L), .Label = c("4X4", 
"HATCH", "SEDAN"), class = "factor"), profile = structure(c(1L, 
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 
1L), .Label = c("FIXED", "FREE", "MOBILE"), class = "factor"), 
    value = c(6440.32, 6287.22, 9324, 7532, 7287.63, 6827.27, 
    6880.48, 7795.15, 7042.51, 2708.41, 1373.69, 6742.87, 7692.65, 
    7692.65, 8116.56, 7692.65, 7692.65, 7692.65, 7962.65, 8116.56, 
    5691.12, 2434, 8343, 7727.73, 7692.65, 7721.15, 1944.38, 
    6044.23, 8633.65, 7692.65, 7692.65, 8151.65, 7692.65, 7692.65, 
    2708.41, 3271.45, 3333.82, 1257.48, 6223.13, 7692.65, 6955.46, 
    7115.46, 7115.46, 7115.46, 7115.46, 6955.46, 7615.46, 2621.21, 
    2621.21, 445.61)), .Names = c("category", "profile", "value"
), class = "data.frame", row.names = c(NA, -50L))

library(ggplot2)

p = ggplot(d, aes(x=d$value, fill=d$category)) + geom_density(alpha=.3)
p + facet_grid(d$profile ~ .)

解决方案

Your problem comes from referring to variables explicitly (i.e. d$profile), not with respect to the data argument in the call to ggplot. There is no need for d$ anywhere.

When faceting using facet_grid or facet_wrap, you need to do so. It is also good practice to do in calls to aes

p = ggplot(d, aes(x=value, fill=category)) + geom_density(alpha=.3)
p + facet_grid(profile ~ .)

这篇关于如何在ggplot2中正确使用facet_grid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆