multiple legends

56 views
Skip to first unread message

Troels Ring

unread,
Feb 8, 2018, 2:56:59 AM2/8/18
to ggplot2
Hi friends - I have problems formating the legend title  with two legends referring to different aesthetics as shown below -

windows(record=TRUE)
library(ggplot2)
set.seed(5234)
RAT <- seq(1,10)
(nglom <- sample(c(3:15),10))
#[[1] 14 11  6 10  7 12  4  9 13 15
source("C:/Users/Bruger/Dropbox/projekt/Stewart/critical care nephrology/r files/mytheme.r")
nkap   <- c()
for (i in 1:length(nglom)) nkap <- c(nkap,sample(c(5:20),nglom[i]))

sum(nglom)     
length(nkap)  

RAT <- rep(RAT,nglom)
NG <- rep(nglom,nglom)
GNUM <- c() 
for (i in 1:10) GNUM <- c(GNUM,c(1:nglom[i]))
ssd <- runif(length(nkap),1,5)

dff <- data.frame(RAT,NG,GNUM,nkap,ssd)

P <- ggplot(data=dff,aes(x=RAT,y=GNUM,size=ssd,col=nkap))+geom_point()
PP <- P +  scale_size_area(nkap) + mytheme + ylab("Glomeruli")+ 
scale_x_continuous(breaks=c(1:10))
PP + labs(col="NC") +  labs(size="sd")

The plot seems to be fine but the legend for size is corrupted - I'd like it to say "sd" - but labs(size="sd") has no effect at all.

How do I handle this? 
All best wishes
Troels

Daniel David

unread,
Feb 8, 2018, 5:52:01 AM2/8/18
to ggplot2
xlab()  

ylab()

ggtitle()

are I think what you are wanting

Troels Ring

unread,
Feb 8, 2018, 6:31:35 AM2/8/18
to ggplot2

Thanks a lot - but this is not the issue. In the code  I use the aesthetics to label the glomeruli from each of 10 rats in the simulated data. For each glomerulus I have a "sd" and a number of capillaries - I can make the circle size to reflect sd and the color to reflect number of capillaries (in that glomerulus) and the plot makes the scales for these OK - only the legend for size is corrupted. Perhaps I need to go into theme or whatever - just can't see it.

Best wishes

Troels

Troels Ring

unread,
Feb 8, 2018, 1:37:03 PM2/8/18
to ggplot2
In the original script there was an unintended and inconsequential call to mytheme - removing this doesn't solve the problem


Den torsdag den 8. februar 2018 kl. 08.56.59 UTC+1 skrev Troels Ring:

Troels Ring

unread,
Feb 8, 2018, 1:45:30 PM2/8/18
to ggplot2


Den torsdag den 8. februar 2018 kl. 08.56.59 UTC+1 skrev Troels Ring:
SOLVED:
Sorry I found the error: the code should simply be
dff <- data.frame(RAT,NG,GNUM,nkap,ssd)

P <- ggplot(data=dff,aes(x=RAT,y=GNUM,size=ssd,col=nkap))+geom_point()
PP <- P  + ylab("Glomeruli")+ 
scale_x_continuous(breaks=c(1:10))
PP + labs(col="NC") +  labs(size="sd")

Thanks a lot
 
Reply all
Reply to author
Forward
0 new messages