【发布时间】:2016-10-07 09:35:35
【问题描述】:
我正在通过R for Data Science Manual 工作,目前正在完成第 3 章。我正在尝试找到一种方法来生成一个将不同类型的自动和手动变速器组合成两个图的图,而不是我目前的图:
# Install necessary packages
install.packages("tidyverse")
library(tidyverse)
# Create the plot
fuelbytrans <- ggplot(data = mpg) +
geom_jitter(
mapping = aes(x = displ, y = hwy, colour = fl),
size = 0.75) +
# Change labels for title and x and y axes
labs(
title = "Drivstofforbruk iht. datasettet «mpg» fordelt på girkasse og motorvolum",
x = "Motorvolum",
y = "Am. mil per gallon")
# Run it
fuelbytrans
# Set colours and labels for fuel legend and position it on the bottom
# e (etanol), d (diesel), r (regular [bensin, lavoktan]), p (premium [bensin, høyoktan]),
# c (CNG)
cols <- c( #kilde: http://colorbrewer2.org/#type=diverging&scheme=PRGn&n=5
"c" = "yellow",
"d" = "red",
"e" = "black",
"p" = "blue",
"r" = "darkgreen"
)
labels_fuel <- fuelbytrans +
scale_colour_manual(
name = "Drivstoff",
values = cols,
breaks = c("c", "d", "e", "p", "r"),
labels = c("CNG",
"diesel",
"etanol",
"bensin,\nhøyoktan",
"bensin,\nlavoktan")) +
theme(legend.position = "bottom",
legend.background = element_rect(
fill = "gray90",
size = 2,
linetype = "dotted"
))
# Run it
labels_fuel
# Wrap by transmission type
labels_fuel + facet_wrap(~ trans, nrow = 1)
如您所见,我得到的是 8 列自动变速箱和 2 列手动变速箱;我想要的只是两列,一列用于自动,另一列用于手动,连接图。我目前不知道如何做到这一点,希望得到所有帮助。
如果有任何信息缺失、应该以不同方式书写或可以改进,请告知。
我正在运行 RStudio 0.99.902。我对 R 很陌生。
【问题讨论】:
标签: r dataframe concatenation facet-wrap