2015-10-20 12 views
5

में किंवदंती किंवदंती मेरे पास और ggplot2 में एक stat_function का प्लॉट है। क्या एक किंवदंती दिखाने का कोई तरीका है?ggplot2

df <- data.frame("x"=c(1:5), "a"=c(1,2,3,3,3), "b"=c(1,1.1,1.3,1.5,1.5)) 
df <- melt(df, "x") 
p <- ggplot(df, aes(x=x, y=value)) + 
    geom_point(aes(colour=variable, shape=variable)) + 
    stat_function(aes(colour="log2(x)"), fun=log2) 

enter image description here

मैं ब्लू लाइन और दो रंग का आकार के साथ एक एकल कथा करना चाहते हैं। मैंने

scale_colour_discrete(name="legend", breaks=c("a", "b", "log2(x)")) + 
scale_shape_discrete(name="legend", breaks=c("a", "b")) 

लेकिन यह काम नहीं करता है। क्या यह स्वचालित रूप से या हाथ से करने का कोई तरीका है?

अग्रिम धन्यवाद।

उत्तर

5

शायद एक आसान विकल्प के रूप में इस override.aes उपयोग करने के लिए है:

ggplot(df, aes(x = x, y = value)) + 
    geom_point(aes(colour = variable, shape = variable), size = 3) + 
    stat_function(aes(colour = "log2(x)"), fun = log2, size = 1.5) + 
    guides(shape = FALSE, 
     colour = guide_legend(override.aes = list(shape = c(16, 17, NA), 
                linetype = c("blank", "blank", "solid")))) 

जिसके परिणामस्वरूप:

enter image description here

3

अपने वक्र के लिए आकार प्रतीक और अपने अंक के लिए एक रिक्त पंक्ति के रूप में एक . निर्दिष्ट करें:

p <- ggplot(df, aes(x=x, y=value)) + 
    geom_point(aes(colour=variable, shape=variable, linetype = variable), size = 3) + 
    stat_function(aes(colour="log2(x)", shape = "log2(x)", linetype = "log2(x)"), fun=log2) + 
    scale_shape_manual(values = setNames(c(16, 17, 46), c("a", "b", "log2(x)"))) + 
    scale_linetype_manual(values = setNames(c(0, 0, 1), c("a", "b", "log2(x)"))) 
print(p) 

resulting plot

+0

धन्यवाद, यह काम करता है। लेकिन मुझे लगता है कि @ जैप का जवाब अधिक "आधिकारिक" तरीका है। – dbrettschneider

+0

निश्चित रूप से, अगर आप इसे याद कर सकते हैं, जो मैं नहीं कर सकता। – Roland

+0

मैं वर्तमान में स्वीकृत उत्तर पर इस विकल्प की अनुशंसा करता हूं। गाइड को ओवरराइड करने से मानव त्रुटि का खतरा होता है, जिससे गलत मार्गदर्शिकाएं होती हैं जो मूल्यों को गलत तरीके से लेबल करती हैं। हालांकि, इस जवाब के साथ, मार्गदर्शिका हमेशा सटीक रहेगी, भले ही गलतियों के विनिर्देशन में गलतियां हों। –