2016-02-26 6 views
5

मुझे डेटा को बहुत अलग रेंज मानों के साथ साजिश करना है। मैं facet_grid(variable ~ ., scales = "free") विकल्प के साथ ggplot2 के पहलू डिजाइन का उपयोग कर रहा हूं। हालांकि, मैं वाई अक्ष पर ब्रेक के मान सेट करना चाहता हूं जैसे कि सभी चर के लिए ब्रेक c(0, max(variable)/2, max(variable)) हैं। मैंने scale_y_continuous का उपयोग करने का प्रयास किया, लेकिन यह काम नहीं किया।ggplot2 का उपयोग कर एक मुखौटा साजिश में वाई अक्ष ब्रेक को कैसे परिभाषित किया जाए?

प्रतिलिपि प्रस्तुत करने योग्य उदाहरण:

v1 <- sample(rnorm(100, 10, 1), 30) 
v2 <- sample(rnorm(100, 20, 2), 30) 
v3 <- sample(rnorm(100, 50, 5), 30) 
fac1 <- factor(rep(rep(c("f1", "f2", "f3"), each = 10), 3)) 

library(reshape2) 
library(ggplot2) 
df1 <- melt(data.frame(fac1, v1, v2, v3)) 
ggplot(df1, aes(fac1, value, group = variable)) + 
    geom_point() + 
    facet_grid(variable ~ ., scales = "free") + 
    theme_bw() 
+0

ggplot मुद्दा [xlim निर्दिष्ट करें और प्रत्येक पहलू के लिए ylim अलग] (https://github.com/hadley/ggplot2/issues/187) चलता है कि यह संभव नहीं है। 24 फरवरी 2014 से @ हैडली की टिप्पणी भी देखें: "यह एक महान विशेषता की तरह लगता है, लेकिन दुर्भाग्यवश हमारे पास इसका समर्थन करने के लिए वर्तमान में विकास बैंडविड्थ नहीं है"। – Henrik

+0

संबंधित एसओ पोस्ट: [आप अलग-अलग पहलुओं के लिए अलग-अलग पैमाने सीमा कैसे निर्धारित करते हैं?] (Http://stackoverflow.com/questions/4276218/how-do-you-set- अलग- स्केल-limits-for- dififferent-facets) और [क्या अभी तक ggplot में व्यक्तिगत पहलुओं के लिए अलग-अलग अक्ष ब्रेक/सीमाएं संभव हैं?] (http://stackoverflow.com/questions/18819333/is-it-yet-possible-to-have- भिन्न -एक्सिस-ब्रेक-सीमा-व्यक्तिगत-पहलुओं के लिए) – Henrik

+0

धन्यवाद। मैं grid.arrange के साथ शायद एक और समाधान खोजने की कोशिश करेंगे? – user34771

उत्तर

1

यह y टूट जाता है 0 पर सेट के साथ तीन अलग-अलग भूखंडों ड्रॉ, 0.5 * अधिकतम (मूल्य), और अधिकतम (मान)। तीन भूखंडों को gtable rbind फ़ंक्शन का उपयोग करके जोड़ा जाता है, फिर खींचा जाता है।

v1 <- sample(rnorm(100, 10, 1), 30) 
v2 <- sample(rnorm(100, 20, 2), 30) 
v3 <- sample(rnorm(100, 50, 5), 30) 
fac1 <- factor(rep(rep(c("f1", "f2", "f3"), each = 10), 3)) 

library(reshape2) 
library(ggplot2) 
library(grid) 
library(gridExtra) 

df1 <- melt(data.frame(fac1, v1, v2, v3)) 

# Draw the three charts, each with a facet strip. 
# But drop off the bottom margin material 
dfp1 = subset(df1, variable == "v1") 
p1 = ggplot(dfp1, aes(fac1, value, group = variable)) + 
    geom_point() + 
    facet_grid(variable ~ .) + 
    scale_y_continuous(limits = c(0, ceiling(max(dfp1$value))), 
     breaks = c(0, ceiling(max(dfp1$value))/2, ceiling(max(dfp1$value)))) + 
    theme_bw() 
g1 = ggplotGrob(p1) 
pos = g1$layout[grepl("xlab-b|axis-b", g1$layout$name), "t"] 
g1 = g1[-pos, ] 

# Drop off the bottom margin material 
dfp2 = subset(df1, variable == "v2") 
p2 = ggplot(dfp2, aes(fac1, value, group = variable)) + 
    geom_point() + 
    facet_grid(variable ~ .) + 
    scale_y_continuous(limits = c(0, ceiling(max(dfp2$value))), 
    breaks = c(0, ceiling(max(dfp2$value))/2, ceiling(max(dfp2$value)))) + 
    theme_bw() 
g2 = ggplotGrob(p2) 
g2 = g2[-pos,] 

dfp3 = subset(df1, variable == "v3") 
p3 = ggplot(dfp3, aes(fac1, value, group = variable)) + 
    geom_point() + 
    facet_grid(variable ~ .) + 
    scale_y_continuous(limits = c(0, ceiling(max(dfp3$value))), 
    breaks = c(0, ceiling(max(dfp3$value))/2, ceiling(max(dfp3$value)))) + 
    theme_bw() 
g3 = ggplotGrob(p3) 

# Combine the three gtables 
g = rbind.gtable(g1, g2, size = "max") 
g = rbind.gtable(g, g3, size = "max") 

# Draw the plot 
grid.newpage() 
grid.draw(g) 

enter image description here

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