2012-09-28 4 views
9

Ich erstelle Boxplots mit ggplot und möchte die Stichprobengröße darstellen, die zu jeder Box beiträgt. In der Basis plot Funktion gibt es die varwidth Option. Hat es ein Äquivalent in ggplot?Gibt es in ggplot ein Äquivalent zur Varwidth-Option in plot?

Zum Beispiel in Grundstück

data <- data.frame(rbind(cbind(rnorm(700, 0,10), rep("1",700)), 
         cbind(rnorm(50, 0,10), rep("2",50)))) 
data[ ,1] <- as.numeric(as.character(data[,1])) 
plot(data[,1] ~ as.factor(data[,2]), varwidth = TRUE) 

enter image description here

+3

Ich scheine erinnere mich an jemanden, der dies vor einiger Zeit auf der Mailing-Liste gefragt hat und ihnen wurde gesagt, dass es w wie nicht möglich. Ich sehe in den Ausgaben von GitHub nichts, was darauf hinweist, also könnte es immer noch nicht möglich sein. (Eine Alternative ist die Verwendung von Füllfarben.) – joran

+0

Mit ggplot nicht möglich, wenn Sie nur ein Diagramm erstellen, können Sie es möglicherweise in Illustrator oder etwas Ähnlichem ändern – by0

+1

@joran Ich habe aus bitterer Erfahrung gelernt, dass etwas in R einfach unmöglich ist dient als Köder für jemanden, der dir Unrecht beweist. In diesem Fall bot der mwathy @ kohske eine Umgehungslösung. – Andrie

Antwort

7

Nicht elegant, aber Sie können dies tun, indem:

data <- data.frame(rbind(cbind(rnorm(700, 0,10), rep("1",700)), 
                         cbind(rnorm(50, 0,10), rep("2",50)))) 
data[ ,1] <- as.numeric(as.character(data[,1])) 
w <- sqrt(table(data$X2)/nrow(data)) 
ggplot(NULL, aes(factor(X2), X1)) + 
    geom_boxplot(width = w[1], data = subset(data, X2 == 1)) + 
    geom_boxplot(width = w[2], data = subset(data, X2 == 2)) 

enter image description here

Wenn Sie mehrere Ebenen für X2 dann kannst du ohne h auskommen ardcoding alle Ebenen:

ggplot(NULL, aes(factor(X2), X1)) + 
    llply(unique(data$X2), function(i) geom_boxplot(width = w[i], data = subset(data, X2 == i))) 

Auch können Sie eine Feature-Anfrage schreiben: https://github.com/hadley/ggplot2/issues

2

Die aktuellen Versionen von ggplot2 (V 2.1.0) enthält nun eine varwidth Option:

data <- data.frame(rbind(cbind(rnorm(700, 0,10), rep("1",700)), 
        cbind(rnorm(50, 0,10), rep("2",50)))) 
data$X1 <- as.numeric(as.character(data$X1)) 
ggplot(data = data, aes(x = X2, y = X1)) + 
    geom_boxplot(varwidth = TRUE) 

Example output plot from ggplot2

Verwandte Themen