2013-07-16 13 views
6

Sto usando il seguente codice per creare una mappa con punti proporzionali a una caratteristica esterna (Totale), ma vorrei cambiare la larghezza del marker.Cambia spessore di un marker in ggplot2

p <- ggplot() 
p <- p + geom_polygon(data=all_states, aes(x=LONG*-1, y=LAT, group = ID),colour="black",    fill="white") 
p <- p + geom_point(data=mydata, aes(x=long*-1, y=lat, size = Total),color="mediumblue",  shape=1) + 
scale_size(range = c(1,11), name="Sells Volume")+ 
labs(title="Reglone SL")+ 
xlab(" ")+ 
ylab(" ") 
p 
+0

Si dovrebbe [fare un esempio riproducibile] (http://stackoverflow.com/questions/5963269/ how-to-make-a-great-r-riproducibile-esempio). – agstudy

+0

Sono d'accordo con Agstudy. Per favore includi anche un po 'più di descrizione su cosa intendi ... intendi il marcatore prodotto da 'geom_point'? Li stai già ridimensionando con la variabile 'Total', quindi li vuoi tutti più grandi? Vuoi loro di più dimensioni? – Justin

risposta

16

È lo spessore del limite di un punto vuoto che si desidera modificare? Può essere fatto con grid.edit dal pacchetto grid.

library(ggplot2) 
library(grid) 

ggplot(data = data.frame(x = 1:10, y = 1:10), aes(x=x, y=y)) + 
    geom_point(size = 10, shape = 1) 

grid.force() # To make the grobs visible to grid editing tools 

grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = seq(1, 5.5, .5))) 

enter image description here

EDIT per ottenere le chiavi della legenda per abbinare i punti

library(ggplot2) 
library(grid) 
library(gtable) 

p = ggplot(data = data.frame(x = 1:10, y = 1:10, c = c(rep("a", 5), rep("b", 5))), 
    aes(x=x, y=y, colour = c)) + 
    geom_point(shape = 1, size = 10) 

lwd = 8 # Set line width 

g = ggplotGrob(p); dev.off() # Get the plot grob 

# Get the indices for the legend: t = top, r = right, ... 
indices <- c(subset(g$layout, name == "guide-box", select = t:r)) 

# Get the row number of the legend in the layout 
rn <- which(g$layout$name == "guide-box") 

# Extract the legend 
legend <- g$grobs[[rn]] 

# Get the legend keys 
pointGrobs = which(grepl("points", legend$grobs[[1]]$grobs)) 

# Check them out - no line width set 
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]]) 

# Set line width 
for (i in pointGrobs) legend$grobs[[1]]$grobs[[i]]$gp$lwd = lwd 

# Check them out - line width set 
# for (i in pointGrobs) str(legend$grobs[[1]]$grobs[[i]]) 

# Put the modified legend back into the plot grob 
g = gtable_add_grob(g, legend, t=indices$t, l=indices$l) 

# g$grobs[[4]]$children[[2]]$gp$lwd = lwd # Alternative for setting lwd for points in the plot 

grid.newpage() 
grid.draw(g) 

grid.force() # To make the grobs visible to grid editing tools 

grid.edit("geom_point.points", grep = TRUE, gp = gpar(lwd = lwd)) 
+1

'grid.edit' è nel pacchetto' grid' – baptiste

+2

un'alternativa con ggplot2 è la sovrapposizione di due livelli: uno con punti pieni di diametro crescente e uno in alto con punti più piccoli e riempimento grigio. – baptiste

+0

Cambia solo lo spessore dei punti, non le dimensioni nella legenda. Come potrei aumentare quest'ultimo per abbinare il primo? – wdkrnls