Skip to content Skip to sidebar Skip to footer

Legend Getting Added Again on Map Initialzation Lerafle

ggplot2 legend : Easy steps to alter the position and the appearance of a graph fable in R software

  • Data
  • Example of plot
  • Alter the legend position
  • Change the fable championship and text font styles
  • Modify the background color of the legend box
  • Change the social club of legend items
  • Remove the plot legend
  • Remove slashes in the legend of a bar plot
  • guides() : fix or remove the legend for a specific aesthetic
    • Default plot without guide specification
    • Modify the legend position for multiple guides
    • Change the order for multiple guides
    • Remove a fable for a particular artful
  • Infos

The goal of this R tutorial is to describe how to change the legend of a graph generated using ggplot2 package.


Data

ToothGrowth data is used in the examples below :

                # Convert the variable dose from numeric to factor variable ToothGrowth$dose <- as.factor(ToothGrowth$dose) caput(ToothGrowth)              
                ##    len supp dose ## 1  4.2   VC  0.five ## 2 11.5   VC  0.5 ## three  7.3   VC  0.5 ## 4  5.eight   VC  0.5 ## 5  6.4   VC  0.five ## 6 ten.0   VC  0.5              

Make certain that the variable dose is converted every bit a factor variable using the higher up R script.

Case of plot

                library(ggplot2) p <- ggplot(ToothGrowth, aes(x=dose, y=len, fill up=dose)) +    geom_boxplot() p              

ggplot2 legend, graph, R software

Modify the fable position

The position of the legend can be changed using the function theme() as follow :

                p + theme(fable.position="height") p + theme(legend.position="lesser")              

ggplot2 legend, graph, R software ggplot2 legend, graph, R software

The allowed values for the arguments legend.position are : "left","superlative", "right", "lesser".

Note that, the argument legend.position can be also a numeric vector c(10,y). In this case information technology is possible to position the fable inside the plotting area. ten and y are the coordinates of the legend box. Their values should exist between 0 and 1. c(0,0) corresponds to the "lesser left" and c(1,ane) corresponds to the "acme right" position.

                p + theme(legend.position = c(0.viii, 0.ii))              

ggplot2 legend, graph, R software

Change the fable title and text font styles

                # legend title p + theme(legend.title = element_text(colour="blue", size=ten,                                        face="bold")) # legend labels p + theme(legend.text = element_text(colour="blue", size=10,                                       face="bold"))              

ggplot2 legend, graph, R software ggplot2 legend, graph, R software

Change the groundwork color of the fable box

                # legend box background colour p + theme(legend.background = element_rect(fill="lightblue",                                    size=0.5, linetype="solid")) p + theme(legend.background = element_rect(fill="lightblue",                                   size=0.5, linetype="solid",                                    colour ="darkblue"))              

ggplot2 legend, graph, R software ggplot2 legend, graph, R software

Change the order of fable items

To change the order of items to "2", "0.5", "1" :

                p + scale_x_discrete(limits=c("2", "0.5", "1"))              

ggplot2 legend, graph, R software

Remove the plot legend

                # Remove just the legend title p + theme(fable.title = element_blank()) # Remove the plot legend p + theme(legend.position='none')              

ggplot2 legend, graph, R software ggplot2 legend, graph, R software

Remove slashes in the fable of a bar plot

                # Default plot ggplot(information=ToothGrowth, aes(x=dose, make full=dose)) + geom_bar() # Alter bar plot border colour,  # but slashes are added in the legend ggplot(data=ToothGrowth, aes(x=dose, fill up=dose)) +   geom_bar(color="black") # Hide the slashes:    #1. plot the bars with no border color,   #2. plot the bars again with edge color, merely with a blank fable. ggplot(data=ToothGrowth, aes(x=dose, fill=dose))+    geom_bar() +    geom_bar(colour="blackness", show_guide=FALSE)              

ggplot2 legend, graph, R software ggplot2 legend, graph, R software ggplot2 legend, graph, R software

guides() : fix or remove the legend for a specific aesthetic

It's possible to employ the function guides() to prepare or remove the legend of a particular aesthetic(fill up, color, size, shape, etc).

mtcars data sets are used :

                # Prepare the information : catechumen cyl and gear to factor variables mtcars$cyl<-as.factor(mtcars$cyl) mtcars$gear <- as.factor(mtcars$gear) caput(mtcars)              
                ##                    mpg cyl disp  hp drat    wt  qsec vs am gear carb ## Mazda RX4         21.0   6  160 110 3.90 ii.620 16.46  0  one    4    4 ## Mazda RX4 Wag     21.0   six  160 110 iii.xc 2.875 17.02  0  ane    iv    four ## Datsun 710        22.8   4  108  93 3.85 two.320 xviii.61  1  one    4    1 ## Hornet 4 Bulldoze    21.4   half dozen  258 110 3.08 3.215 19.44  1  0    3    i ## Hornet Sportabout eighteen.7   8  360 175 3.15 3.440 17.02  0  0    3    2 ## Valiant           18.ane   6  225 105 2.76 iii.460 20.22  1  0    3    one              

Default plot without guide specification

The R lawmaking below creates a scatter plot. The colour and the shape of the points are adamant by the factor variables cyl and gear, respectively. The size of the points are controlled by the variable qsec.

                  p <- ggplot(data = mtcars,      aes(ten=mpg, y=wt, color=cyl, size=qsec, shape=gear))+     geom_point() # Print the plot without guide specification p                

ggplot2 legend, graph, R software

Change the fable position for multiple guides

                  # Change the legend position p +theme(legend.position="bottom")                

ggplot2 legend, graph, R software

                  # Horizontal fable box p +theme(legend.position="bottom", legend.box = "horizontal")                

ggplot2 legend, graph, R software

Change the lodge for multiple guides

The function guide_legend() is used :

                  p+guides(colour = guide_legend(order=1),          size = guide_legend(order=ii),          shape = guide_legend(gild=3))                

ggplot2 legend, graph, R software

If a continuous color is used, the gild of the color guide can be inverse using the function guide_colourbar() :

                  qplot(data = mpg, x = displ, y = cty, size = hwy,       color = cyl, shape = drv) +   guides(colour = guide_colourbar(gild = 1),          blastoff = guide_legend(order = ii),          size = guide_legend(order = 3))                

ggplot2 legend, graph, R software

Remove a legend for a particular artful

The R code below removes the legend for the aesthetics color and size :

                  p+guides(color = FALSE, size = Simulated)                

ggplot2 legend, graph, R software

Removing a particular legend tin can be washed also when using the functions scale_xx. In this case the argument guide is used as follow :

                  # Remove legend for the point shape p+scale_shape(guide=FALSE) # Remove legend for size p +scale_size(guide=FALSE) # Remove fable for color p + scale_color_manual(values=c('#999999','#E69F00','#56B4E9'),                        guide=FALSE)                

ggplot2 legend, graph, R software ggplot2 legend, graph, R software ggplot2 legend, graph, R software

Infos

This assay has been performed using R software (ver. three.ane.0) and ggplot2 (ver. 1.0.0)


Enjoyed this article? I'd be very grateful if you'd assistance information technology spread by emailing information technology to a friend, or sharing it on Twitter, Facebook or Linked In.

Prove me some love with the like buttons below... Thank you and please don't forget to share and comment below!!

Avez vous aimé cet commodity? Je vous serais très reconnaissant si vous aidiez à sa improvidence en fifty'envoyant par courriel à un ami ou en le partageant sur Twitter, Facebook ou Linked In.

Montrez-moi un peu d'amour avec les like ci-dessous ... Merci et n'oubliez pas, s'il vous plaît, de partager et de commenter ci-dessous!



johnsonleared43.blogspot.com

Source: http://www.sthda.com/english/wiki/ggplot2-legend-easy-steps-to-change-the-position-and-the-appearance-of-a-graph-legend-in-r-software

Post a Comment for "Legend Getting Added Again on Map Initialzation Lerafle"