2012-04-26 14 views
27

मैं ggplot2 में एक डोडेड बारप्लॉट बना रहा हूं और एक समूह में शून्य गणना है जिसे मैं प्रदर्शित करना चाहता हूं। मुझे कुछ समय पहले HERE पर यह देखकर याद आया और लगा कि scale_x_discrete(drop=F) काम करेगा। यह dodged सलाखों के साथ काम करने के लिए प्रतीत नहीं होता है। मैं शून्य गणना कैसे दिखा सकता हूं?शून्य गिनती न छोड़ें: डोडेड बारप्लॉट

उदाहरण के लिए, नीचे दिए गए प्लॉट में (नीचे कोड), टाइप 8 ~ समूह 4 में कोई उदाहरण नहीं है। मैं अभी भी साजिश को बार को खत्म करने के बजाय शून्य गिनती के लिए रिक्त स्थान प्रदर्शित करना चाहता हूं। मैं यह कैसे कर सकता हूँ?

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 

उत्तर

10

एक ही रास्ता मैं के बारे में पता मायने रखता है पूर्व की गणना और एक डमी पंक्ति जोड़ने के लिए है:

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

मैंने सोचा था कि बजाय stat_bin(drop = FALSE,geom = "bar",...) का उपयोग करेगा काम, लेकिन स्पष्ट रूप से यह नहीं करता है।

+0

नहीं के रूप में आसान के रूप में मुझे उम्मीद थी, लेकिन मेरी खोज तो मैं यह कुछ नए सिरे ले जाएगा सोचा जाना चाहिए था में एक उपयुक्त जवाब नहीं मिल सका। धन्यवाद जोरेन। बहुत अच्छी तरह से काम किया +1 –

+0

@ टायलर रिंकर ईमानदारी से, मुझे लगता है कि 'stat_bin (ड्रॉप = गलत, geom = "bar", स्थिति = "डॉज", ...) '_should_ ऐसा करें; कम से कम, दस्तावेज़ीकरण दृढ़ता से सुझाव देता है कि यह होगा। मेलिंग सूची पर अधिक जानकार लोगों से सुनने के लिए मैं बहुत उत्सुक हूं कि ऐसा क्यों नहीं होता है। – joran

+0

मैं अभी एक परियोजना पर काम कर रहा हूं लेकिन मैं इसे बाद में सूची में फेंक दूंगा और यहां वापस रिपोर्ट करूंगा। –

15

अपडेट किया गयाgeom_bar() जरूरतों stat = "identity"

क्या इसके लायक है के लिए: गिनती की मेज, डैट, ऊपर लागू नहीं होता है। कभी-कभी, इसके बजाय एक स्पष्ट 0 होना उपयोगी होता है; उदाहरण के लिए, यदि अगला कदम सलाखों के ऊपर गणना करना है। निम्नलिखित कोड सिर्फ यही है, हालांकि यह शायद जोरेन की तुलना में आसान नहीं है। इसमें दो चरण शामिल हैं: dcast का उपयोग करके गणनाओं का क्रॉसस्टाबलेशन प्राप्त करें, फिर melt का उपयोग करके तालिका को पिघलाएं, इसके बाद ggplot() सामान्य रूप से।

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

यह घाव थोड़ा अच्छा है। मैं पहले ही ग्राफिक पूरा कर चुका हूं और इसमें कुछ हैकिश जंक लिया लेकिन इससे उन समस्याओं को हल किया गया। अच्छी प्रतिक्रिया। +1 –

7

मैं इस एक ही सवाल पूछा, लेकिन मैं केवल, data.table उपयोग करने के लिए के रूप में यह बहुत बड़ा डेटा सेट के लिए एक तेजी से समाधान है चाहता था। मैंने डेटा पर नोट्स शामिल किए ताकि वे कम अनुभवी हों और समझ सकें कि मैंने जो किया है, मैंने ऐसा क्यों किया है। यहाँ कैसे मैं mtcars डेटा सेट चालाकी से है:

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" 

और यहाँ कॉल कि ग्राफ

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) 

उत्पादित है और यह इस ग्राफ का उत्पादन: इसके अलावा

Cylinder Graph

, यदि निरंतर डेटा है, तो diamonds डेटा सेट (एमनेल के लिए धन्यवाद) में:

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] 

फिर CJ का उपयोग करना भी काम करेगा।

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") 

हमें इस ग्राफ देते:

Diamonds Fixed

0

आप table() समारोह है, जो के लिए एक कारक की घटनाओं की संख्या की गणना की सुविधा सभी इसका स्तर दोहन कर सकते हैं

# 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

यहां बताया गया है कि आप पहले सारणी सारणी बनाये बिना इसे कैसे कर सकते हैं।
यह मेरे क्रैन बनाम (2.2.1) में काम नहीं करता है लेकिन ggplot (2.2.1.900) के नवीनतम विकास संस्करण में मुझे कोई समस्या नहीं थी।

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

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

संबंधित मुद्दे