如何在气泡图的ggplot2中有多重标签 [英] how to have multple labels in ggplot2 for bubble plot

查看:854
本文介绍了如何在气泡图的ggplot2中有多重标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的问题太简单或者太无聊,请原谅我,我刚刚开始研究R.我已经搜索并尝试了很多次,但是我无法拥有多个标签。
这就是我的代码的样子:

$ p $ datato< -read.table(forS.txt,header =真)

g <-ggplot(datato)

g +
geom_point(aes(x = Point1,y = Alphabets,size = D1),fill = (aes(x = Point2,y = Alphabets,size = D2),fill =gold2,shape = 21)+
geom_point(aes(x = axis3,y = Alphabets,size = D3),fill =lightpink4,shape = 21)+
scale_size(range = c(0,20),name =)+ theme(axis.text。 x = element_text(size = 15,face =bold),axis.text.y = element_text(size = 15,face =bold))+
xlab(Numbers)+ ylab(Alphabets Freq)+ ggtitle(Bubble Chart)+
scale_x_continuous(limits = c(1,15))+
scale_shape_manual(values = 1:3,labels = c(DDD,EEE ,FFF))

我正在用D2和D3绘制D1,字母等。我可以根据需要获得一个不错的泡泡。但最后,我在右侧尺寸上标注 lightpink4 ,这是默认设置,并覆盖了以前的标签。
但是我想在D1的右边显示青色,在右边显示D3的黄色2和D3的lightpink4。
我弄不清楚,如何使用: scale_shape_manual



请帮我理解这一点。 p>

 字母表D1 D2 D3 Point1 Point2 Point3 
A 0.094 0.073 0.11 1 2 3


B 0.019 0.08 0.09 1 2 3

C 0.086 0.059 0.05 1 2 3

D 0.03 0.021 0.09 1 2 3


解决方案


  • 将数据重塑为长格式。对于 ggplot2

  • ,您希望 color 而非 fill

  • 您需要 scale_colour_manual 而不是 scale_shape_manual



  • 这应该有效:

      library(ggplot2)
    library(reshape2)

    数据集< - data.frame(
    Alphabets = runif(18),
    D1 (6),
    D2 = runif(6),
    D3 = runif(6)


    熔化< - 熔化(
    数据集,
    id.vars =Alphabets,
    measure.vars = c(D1,D2,D3)



    ggplot(fused,aes(x = variable,y = Alphabets,size = value,color = variable))+
    geom_point()+
    scale_colour_manual(values = c(D1=青色,D2=gold2,D3=lightpink4))

    < img src =https://rud.is/dl/22598744.pngalt =plot>


    Pardon me if my question is too simple or silly, I have just started working on R. I have searched and tried many times but I am unable to have multiple labels. This is what my code looks like:

    datato<-read.table("forS.txt",header=TRUE)
    
    g<-ggplot(datato)
    
    g+
    geom_point(aes(x=Point1,y=Alphabets,size=D1),fill="cyan",shape=21)+
    geom_point(aes(x=Point2,y=Alphabets,size=D2),fill="gold2",shape=21)+
    geom_point(aes(x=Point3,y=Alphabets,size=D3),fill="lightpink4",shape=21)+
    scale_size(range = c(0, 20),name="") + theme(axis.text.x = element_text(size =    15,face="bold"),axis.text.y = element_text(size = 15,face="bold"))+   
    xlab("Numbers") + ylab("Alphabets Freq")+ggtitle("Bubble Chart")+
    scale_x_continuous(limits = c(1, 15))+
    scale_shape_manual(values=1:3, labels = c("DDD", "EEE", "FFF"))
    

    I am plotting D1 against, alphabets and so with D2 and D3. I get a nice bubble plot as I need. But in the end, I get label for lightpink4 on the right hand size, which is by default and overrides the previous labels. But I want to show cyan is for D1, gold2 for D2 and lightpink4 for D3 on the right hand side. I cannot figure out, how to use: scale_shape_manual

    Please help me understand this.

     Alphabets  D1  D2  D3  Point1  Point2  Point3
    A   0.094   0.073   0.11    1   2   3
    
    
    B   0.019   0.08    0.09    1   2   3
    
    C   0.086   0.059   0.05    1   2   3
    
    D   0.03    0.021   0.09    1   2   3
    

    解决方案

    • reshape your data into a long format. That much more useful with ggplot2
    • you want colour instead of fill
    • you want scale_colour_manual instead of scale_shape_manual

    This should work:

    library(ggplot2)
    library(reshape2)
    
    dataset <- data.frame(
      Alphabets = runif(18), 
      D1 = runif(6), 
      D2 = runif(6), 
      D3 = runif(6)
    )
    
    molten <- melt(
      dataset, 
      id.vars = "Alphabets", 
      measure.vars = c("D1", "D2", "D3")
    )
    
    
    ggplot(molten, aes(x = variable, y = Alphabets, size = value, colour = variable)) +
      geom_point() +
      scale_colour_manual(values = c("D1" = "cyan", "D2" = "gold2", "D3" = "lightpink4"))
    

    这篇关于如何在气泡图的ggplot2中有多重标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆