2017-09-29 3 views
1

Nach dem Einfügen der Legende wird die X-Achse extrem rechts-Tick-Label ("400") abgeschnitten. Zwei Lösungen zum Clipping funktionieren nicht. enter image description hereggplot2: X-Achse extrem-rechtes Hilfsstrichbeschriftung abgeschnitten nach Einfüge-Legende

library(ggplot2) 
textsize=12 
o2b <- colorRampPalette(c("brown", "orange"))(4) 

p <- theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
      panel.background=element_blank(), panel.border=element_blank(), 
      plot.title=element_blank(), 
      legend.background=element_blank(), legend.key=element_blank(), legend.position=c(1,1), 
      legend.justification=c(1,1), legend.text=element_text(size=textsize), legend.title=element_text(size=textsize), 
      axis.line=element_line(colour="black"), axis.text=element_text(size=textsize, colour="black"), 
      axis.title=element_text(size=textsize)) 

p1 <- ggplot(temp1, aes(x=rank, y=rhh, colour=factor(naics_level))) + p + geom_point(shape=1, size=2) + 
    scale_color_manual(values=o2b) + 
    guides(colour = guide_legend(title="Niveau de NAICS", title.position = "left", reverse=T)) + 
    labs(x="Rang des MSA", y="Diversité sectorielle basée sur l'emploi en 2015") 

# Turn off clipping 
library(grid) 

# Neither of the two following commands makes a difference 
gt <- ggplotGrob(p1) 
gt <- ggplot_gtable(ggplot_build(p1)) 

gt$layout$clip[gt$layout$name=="panel"] <- "off" 
grid.draw(gt) 
+2

Versuchen Sie mit 'plot.margin' in' theme' – MikolajM

+0

Dank @MikolajM. 'plot.margin = unit (c (5.5,12,5.5,5.5)," pt ") löste es für mich. – syre

Antwort

0

Basierend auf @ MikolajM Vorschlag, die einfache Lösung ist:

library(ggplot2) 
textsize=12 
o2b <- colorRampPalette(c("brown", "orange"))(4) 

p <- theme(panel.grid.major=element_blank(), panel.grid.minor=element_blank(), 
      panel.background=element_blank(), panel.border=element_blank(), 
      plot.title=element_blank(), plot.margin = unit(c(5.5,12,5.5,5.5), "pt"), 
      legend.background=element_blank(), legend.key=element_blank(), legend.position=c(1,1), 
      legend.justification=c(1,1), legend.text=element_text(size=textsize), legend.title=element_text(size=textsize), 
      axis.line=element_line(colour="black"), axis.text=element_text(size=textsize, colour="black"), 
      axis.title=element_text(size=textsize)) 

p1 <- ggplot(temp1, aes(x=rank, y=rhh, colour=factor(naics_level))) + p + geom_point(shape=1, size=2) + 
    scale_color_manual(values=o2b) + 
    guides(colour = guide_legend(title="Niveau de NAICS", title.position = "left", reverse=T)) + 
    labs(x="Rang des MSA", y="Diversité sectorielle basée sur l'emploi en 2015") 

Ausschalten Clipping irrelevant wird.

Verwandte Themen