Tôi hỏi câu hỏi này giống nhau, nhưng tôi chỉ muốn sử dụng data.table
, vì nó là một giải pháp nhanh hơn cho các tập dữ liệu lớn hơn nhiều. Tôi đã bao gồm các ghi chú trên dữ liệu để những người ít kinh nghiệm và muốn hiểu tại sao tôi đã làm những gì tôi đã làm có thể làm như vậy một cách dễ dàng. Dưới đây là cách tôi thao túng tập mtcars
dữ liệu:
library(data.table)
library(scales)
library(ggplot2)
mtcars <- data.table(mtcars)
mtcars$Cylinders <- as.factor(mtcars$cyl) # Creates new column with data from cyl called Cylinders as a factor. This allows ggplot2 to automatically use the name "Cylinders" and recognize that it's a factor
mtcars$Gears <- as.factor(mtcars$gear) # Just like above, but with gears to Gears
setkey(mtcars, Cylinders, Gears) # Set key for 2 different columns
mtcars <- mtcars[CJ(unique(Cylinders), unique(Gears)), .N, allow.cartesian = TRUE] # Uses CJ to create a completed list of all unique combinations of Cylinders and Gears. Then counts how many of each combination there are and reports it in a column called "N"
Và đây là cuộc gọi mà sản xuất đồ thị
ggplot(mtcars, aes(x=Cylinders, y = N, fill = Gears)) +
geom_bar(position="dodge", stat="identity") +
ylab("Count") + theme(legend.position="top") +
scale_x_discrete(drop = FALSE)
Và nó tạo ra biểu đồ này:
Bên cạnh đó, nếu có dữ liệu liên tục, như vậy trong bộ dữ liệu diamonds
(nhờ vào mnel):
library(data.table)
library(scales)
library(ggplot2)
diamonds <- data.table(diamonds) # I modified the diamonds data set in order to create gaps for illustrative purposes
setkey(diamonds, color, cut)
diamonds[J("E",c("Fair","Good")), carat := 0]
diamonds[J("G",c("Premium","Good","Fair")), carat := 0]
diamonds[J("J",c("Very Good","Fair")), carat := 0]
diamonds <- diamonds[carat != 0]
Sau đó, sử dụng CJ
cũng sẽ hoạt động.
data <- data.table(diamonds)[,list(mean_carat = mean(carat)), keyby = c('cut', 'color')] # This step defines our data set as the combinations of cut and color that exist and their means. However, the problem with this is that it doesn't have all combinations possible
data <- data[CJ(unique(cut),unique(color))] # This functions exactly the same way as it did in the discrete example. It creates a complete list of all possible unique combinations of cut and color
ggplot(data, aes(color, mean_carat, fill=cut)) +
geom_bar(stat = "identity", position = "dodge") +
ylab("Mean Carat") + xlab("Color")
Cho chúng tôi biểu đồ này:
Không dễ dàng như tôi đã hy vọng nhưng không thể tìm ra câu trả lời thích hợp trong tìm kiếm của tôi vì vậy tôi nên đã tìm nó sẽ mất một số làm lại. Cảm ơn Joran. Làm việc rất tốt +1 –
@TylerRinker Thành thật mà nói, tôi cảm thấy như 'stat_bin (drop = FALSE, geom =" bar ", position =" dodge ", ...)' _should_ làm điều này; ít nhất, các tài liệu mạnh mẽ cho thấy rằng nó sẽ. Tôi rất tò mò muốn nghe từ những người hiểu biết nhiều hơn về danh sách gửi thư vì sao nó không. – joran
Tôi đang làm việc trên một dự án ngay bây giờ nhưng tôi sẽ ném nó lên trên danh sách sau và báo cáo lại tại đây. –