2012-04-26 24 views
27

Tôi đang tạo một thanh chắn được né tránh trong ggplot2 và một nhóm có số không bằng 0 mà tôi muốn hiển thị. Tôi nhớ đã nhìn thấy điều này trên HERE một lúc trở lại và tìm được scale_x_discrete(drop=F) sẽ hoạt động. Nó không xuất hiện để làm việc với các thanh được né tránh. Làm thế nào tôi có thể làm cho số không hiển thị?Không được giảm số không: ô thanh được né tránh

Ví dụ: (mã bên dưới) trong ô bên dưới, type8 ~ group4 không có ví dụ. Tôi vẫn muốn âm mưu hiển thị không gian trống cho số không thay vì loại bỏ thanh. Tôi có thể làm cái này như thế nào?

enter image description here

mtcars2 <- data.frame(type=factor(mtcars$cyl), 
    group=factor(mtcars$gear)) 

m2 <- ggplot(mtcars2, aes(x=type , fill=group)) 
p2 <- m2 + geom_bar(colour="black", position="dodge") + 
     scale_x_discrete(drop=F) 
p2 

Trả lời

10

Cách duy nhất tôi biết là trước tính đếm và thêm một hàng giả:

dat <- rbind(ddply(mtcars2,.(type,group),summarise,count = length(group)),c(8,4,NA)) 

ggplot(dat,aes(x = type,y = count,fill = group)) + 
    geom_bar(colour = "black",position = "dodge",stat = "identity") 

enter image description here

Tôi nghĩ rằng việc sử dụng stat_bin(drop = FALSE,geom = "bar",...) thay vào đó sẽ làm việc, nhưng rõ ràng là không.

+0

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 –

+0

@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

+0

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. –

15

Cập nhậtgeom_bar() nhu cầu stat = "identity"

Đối với những gì nó có giá trị: Bảng đếm, dat, trên có chứa NA. Đôi khi, hữu ích khi có 0 rõ ràng thay thế; ví dụ, nếu bước tiếp theo là đặt số lượng trên các thanh. Các mã sau đây không chỉ có vậy, mặc dù nó có lẽ không đơn giản hơn của Joran. Nó liên quan đến hai bước: có được một số đếm chéo bằng cách sử dụng dcast, sau đó làm tan bảng bằng cách sử dụng melt, theo sau là ggplot() như bình thường.

library(ggplot2) 
library(reshape2) 
mtcars2 = data.frame(type=factor(mtcars$cyl), group=factor(mtcars$gear)) 

dat = dcast(mtcars2, type ~ group, fun.aggregate = length) 
dat.melt = melt(dat, id.vars = "type", measure.vars = c("3", "4", "5")) 
dat.melt 

ggplot(dat.melt, aes(x = type,y = value, fill = variable)) + 
    geom_bar(stat = "identity", colour = "black", position = position_dodge(width = .8), width = 0.7) + 
    ylim(0, 14) + 
    geom_text(aes(label = value), position = position_dodge(width = .8), vjust = -0.5) 

enter image description here

+0

Vết thương này trở nên đẹp hơn một chút. Tôi đã hoàn thành các đồ họa và nó đã mất một số rác hackish nhưng điều này giải quyết những vấn đề. Phản ứng tốt đẹp. +1 –

7

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:

Cylinder Graph

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:

Diamonds Fixed

0

Bạn có thể khai thác các tính năng của table() chức năng, trong đó tính số lần xuất hiện của một nhân tố cho tất cả mức của nó

# load plyr package to use ddply 
library(plyr) 

# compute the counts using ddply, including zero occurrences for some factor levels 
df <- ddply(mtcars2, .(group), summarise, 
types = as.numeric(names(table(type))), 
counts = as.numeric(table(type))) 

# plot the results 
ggplot(df, aes(x = types, y = counts, fill = group)) + 
geom_bar(stat='identity',colour="black", position="dodge") 

Results graph

3

Dưới đây là cách bạn có thể thực hiện mà không cần tạo bảng tóm tắt trước.
Nó không hoạt động trong phiên bản CRAN của tôi (2.2.1) nhưng trong phiên bản phát triển mới nhất của ggplot (2.2.1.900), tôi không có vấn đề gì.

ggplot(mtcars, aes(factor(cyl), fill = factor(vs))) + 
    geom_bar(position = position_dodge(preserve = "single")) 

http://ggplot2.tidyverse.org/reference/position_dodge.html

Các vấn đề liên quan