how to have multple labels in ggplot2 for bubble plot

你。 提交于 2020-01-06 20:01:37

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/22598517/how-to-have-multple-labels-in-ggplot2-for-bubble-plot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!