我一直在尝试从此处扩展场景以利用构面(尤其是facet_grid())。
我已经看到了这个例子,但是我似乎无法得到它为我的工作geom_bar()和geom_point()组合。我尝试使用示例中的代码,只是从更改为facet_wrap,facet_grid这似乎也使得第一层未显示。
我是网格和杂项方面的新手,因此,如果有人可以给出一些指导,使P1在y轴左方显示,P2在y轴右方显示,那会很棒。
数据
library(ggplot2)
library(gtable)
library(grid)
library(data.table)
library(scales)
grid.newpage()
dt.diamonds <- as.data.table(diamonds)
d1 <- dt.diamonds[,list(revenue = sum(price),
stones = length(price)),
by=c("clarity","cut")]
setkey(d1, clarity,cut)
p1和p2
p1 <- ggplot(d1, aes(x=clarity,y=revenue, fill=cut)) +
geom_bar(stat="identity") +
labs(x="clarity", y="revenue") +
facet_grid(. ~ cut) +
scale_y_continuous(labels=dollar, expand=c(0,0)) +
theme(axis.text.x = element_text(angle = 90, hjust = 1),
axis.text.y = element_text(colour="#4B92DB"),
legend.position="bottom")
p2 <- ggplot(d1, aes(x=clarity, y=stones, colour="red")) +
geom_point(size=6) +
labs(x="", y="number of stones") + expand_limits(y=0) +
scale_y_continuous(labels=comma, expand=c(0,0)) +
scale_colour_manual(name = '',values =c("red","green"), labels = c("Number of Stones"))+
facet_grid(. ~ cut) +
theme(axis.text.y = element_text(colour = "red")) +
theme(panel.background = element_rect(fill = NA),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.border = element_rect(fill=NA,colour="grey50"),
legend.position="bottom")
尝试组合(基于上面链接的示例) 这在第一个for循环中失败,我怀疑对geom_point.points进行了硬编码,但是我不知道如何使其适合我的图表(或者足够灵活以适合各种
慕尼黑5688855
天涯尽头无女友