How to increase the size of a particular plot symbol?

1,229 views
Skip to first unread message

Marius Hofert

unread,
Aug 29, 2011, 1:34:10 PM8/29/11
to ggplot2
Dear all,

I would like to increase the size of a particular plot symbol (the one
numbered 18). I tried to work with "size=..." but it seems that one
can only increase the size of all plot symbols simultaneously.

Cheers,

Marius


require(ggplot2)
x <- 1:4
y <- x
df <- data.frame(x, y)
my.shapes <- c(15,17,18,20)
ggplot(df, aes(x=x, y=y, shape=as.factor(x))) + geom_point(size=4) +
scale_shape_manual(values=my.shapes, breaks=as.factor(x)) # does
work but increases the size of *all* plot symbols
ggplot(df, aes(x=x, y=y, shape=as.factor(x))) +
geom_point(size=c(2,2,4,2)) +
scale_shape_manual(values=my.shapes, breaks=as.factor(x)) # does
not work: "Error: When _setting_ aesthetics, they may only take one
value. Problems: size"

Dennis Murphy

unread,
Aug 29, 2011, 2:28:58 PM8/29/11
to Marius Hofert, ggplot2
Hi:

How about this?

df <- data.frame(x = 1:4)
df <- mutate(df, y = x, shap = factor(x), sz = c(3, 3, 10, 3))
my.shapes <- c(15,17,18,20)

# Notice that both size and shape are mapped to shap, and that
# both scales have the same title and breaks:
ggplot(df, aes(x=x, y=y, shape= shap, size = shap)) + geom_point() +
scale_shape_manual('x', breaks = levels(df$shap),
values = my.shapes) +
scale_size_manual('x', breaks = levels(df$shap),
values = df$sz)

HTH,
Dennis

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Justin Haynes

unread,
Aug 29, 2011, 2:37:55 PM8/29/11
to Marius Hofert, ggplot2
Its not terribly pretty but:

ggplot() + geom_point(data=df[df$x!=1,], aes(x=x, y=y, shape=as.factor(x)),size=4) +
   geom_point(data=df[df$x==1,],aes(x=x,y=y,shape=as.factor(x)),size=10) +
   scale_shape_manual(values=my.shapes, breaks=as.factor(x))

works.  It does make for a funky legend though.

you can use a dummy to fix it if you want though:

dummy<-df[1,]
dummy[,1]<-NaN

ggplot() + geom_point(data=df[df$x!=1,], aes(x=x, y=y, shape=as.factor(x)),size=4,legend=F) +
   geom_point(data=df[df$x==1,],aes(x=x,y=y,shape=as.factor(x)),size=10,legend=F) +
   geom_point(data=dummy,aes(x=x,y=y,shape=as.factor(x)),size=4,na.rm=T) +
   scale_shape_manual(values=my.shapes, breaks=as.factor(x))


Hope that helps,

Justin

On Mon, Aug 29, 2011 at 10:34 AM, Marius Hofert <marius...@googlemail.com> wrote:

Marius Hofert

unread,
Aug 29, 2011, 4:41:30 PM8/29/11
to Dennis Murphy, ggplot2
Hi,

I tried your approach in a more complicated example (below), but I still couldn't manage to get it done properly.
Below are my trials. The hard part is to get only the symbol sizes changed, but not the line widths. In Trial 3, I get the desired behavior, but a second legend is added. How can I remove that?

Cheers,

Marius

require(devtools)
dev_mode(TRUE)
require(ggplot2)
require(reshape2)

## parameters of some model
p1 <- c(20, 50, 100, 200)
p2 <- c(5, 10, 20, 50, 100)
p3 <- c("group.1","group.2","group.3","group.4","group.5")
p4 <- c(0.2, 0.5, 0.8)

## replications
N <- 1000

## lengths
l1 <- length(p1)
l2 <- length(p2)
l3 <- length(p3)
l4 <- length(p4)

## build result array containing the measurements
arr <- array(rep(NA, l1*l2*l3*l4*N), dim=c(l1, l2, l3, l4, N),
dimnames=list(
p1=p1,
p2=p2,
p3=p3,
p4=p4,
N=1:N))

## fill the result array (dummy here) with values
for(i in 1:l1){
for(j in 1:l2){
for(k in 1:l3){
for(l in 1:l4){
arr[i,j,k,l,] <- i+j+k+l+runif(N)
}
}
}
}

## create molten data
mdf <- melt(arr, formula = . ~ + p1 + p2 + p3 + p4 + N) # create molten data frame
colnames(mdf) # => fine
stopifnot(dim(mdf)==c(l1*l2*l3*l4*N, 6))

## build means (dummy function here)
mdf.mean <- dcast(mdf, p1 + p2 + p3 + p4 ~ "Value", fun.aggregate=mean, value_var="value")
str(mdf.mean)

## add p1 legend labels
my.labs <- lapply(1:4, function(l) bquote(alpha==.(l)))
sym <- c(15,17,18,20)
sym.size <- c(1,1,4,1)

## plot data frame
mdf.mean. <- mdf.mean
mdf.mean.$p1 <- as.factor(mdf.mean.$p1) # convert p1 to factor

## trial 1:
ggplot(mdf.mean., aes(x=p2, y=Value, shape=p1)) + geom_point() +
geom_line(aes(group=p1)) + facet_grid(p3 ~ p4, space="free") +
scale_x_continuous(limits=c(1,max(p2)), breaks=p2, labels=p2) +
scale_shape_manual(values=sym, breaks=p1, labels=my.labs) +
scale_size_manual(values=sym.size, breaks=p1)
## => symbol sizes are not adjusted

## trial 2:
ggplot(mdf.mean., aes(x=p2, y=Value, shape=p1, size=p1)) + geom_point() +
geom_line(aes(group=p1)) + facet_grid(p3 ~ p4, space="free") +
scale_x_continuous(limits=c(1,max(p2)), breaks=p2, labels=p2) +
scale_shape_manual(values=sym, breaks=p1, labels=my.labs) +
scale_size_manual(values=sym.size, breaks=p1)
## => the line widths/sizes are changed, instead of the symbol sizes

## trial 3:
ggplot(mdf.mean., aes(x=p2, y=Value, shape=p1)) + geom_point(aes(size=p1)) +
geom_line(aes(group=p1)) + facet_grid(p3 ~ p4, space="free") +
scale_x_continuous(limits=c(1,max(p2)), breaks=p2, labels=p2) +
scale_shape_manual(values=sym, breaks=p1, labels=my.labs) +
scale_size_manual(values=sym.size, breaks=p1)
## => seems to do the job but a second legend is added

Reply all
Reply to author
Forward
0 new messages