in Education by
Suppose I have a ggplot with more than one legend. mov <- subset(movies, length != "") (p0 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) + geom_point() ) I can turn off the display of all the legends like this: (p1 <- p0 + theme(legend.position = "none")) Passing show_guide = FALSE to geom_point turns off the shape legend. (p2 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) + geom_point(show_guide = FALSE) ) But what if I want to turn off the color legend instead? There doesn't seem to be a way of telling show_guide which legend to apply its behavior to. And there is no show_guide argument for scales or aesthetics. (p3 <- ggplot(mov, aes(year, rating, colour = length, shape = mpaa)) + scale_colour_discrete(show_guide = FALSE) + geom_point() ) # Error in discrete_scale (p4 <- ggplot(mov, aes(year, rating, shape = mpaa)) + aes(colour = length, show_guide = FALSE) + geom_point() ) #draws both legends I want to be able to do something like p0 + guides( colour = guide_legend(show = FALSE) ) but guide_legend doesn't have a show argument. How do I specify which legends get displayed? Select the correct answer from above options

1 Answer

0 votes
by
 
Best answer
To turn off legend for a specific aesthetic, you can use the guides() function as follows: guides(color = FALSE) #To turn off color legend guides(fill = FALSE) #To turn off fill legend guides(shape = FALSE) #To turn off shape legend Or you can also use scale_color_continuous(guide = FALSE) to suppress the color legend since the length is a continuous variable. For example: Plot with all legends: ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) + geom_point(aes(color = vs)) + geom_point(aes(shape = factor(cyl))) + geom_line(aes(linetype = factor(gear))) + geom_smooth(aes(fill = factor(gear), color = gear)) + theme_bw() Plot without the color legend: ggplot(data = mtcars, aes(x = mpg, y = disp, group = gear)) + geom_point(aes(color = vs)) + geom_point(aes(shape = factor(cyl))) + geom_line(aes(linetype = factor(gear))) + geom_smooth(aes(fill = factor(gear), color = gear)) + theme_bw() + guides(color = FALSE) If you want to explore more in R programming then watch this R programming tutorial for beginner:

Related questions

0 votes
    I'm trying to keep the legend of one layer (smooth) and remove the legend of the other (point). I have tried ... = gear)) + theme_bw() Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    A very newbish question, but say I have data like this: test_data...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Hi this simple code (and all my scripts from this morning) has started giving me an off-center title in ggplot2 Ubuntu ... the above this morning to try and fix this.... dat...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have used the following ggplot command: ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ... but cannot figure out how. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    _________ describe the type of plot you will produce. (a) geoms (b) ggplot (c) fplot (d) gplot This ... Data Analysis of R Programming Select the correct answer from above options...
asked Feb 9, 2022 in Education by JackTerrance
0 votes
    How can I plot two graphs in same plot in R, I am using this command to perform my task but it isn’t working. x...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    I have a huge vector which has a couple of NA values, and I'm trying to find the max value in that vector ( ... I can compute the max? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Many times I have seen the set.seed function in R, before starting the program. I know it's basically used for ... need to set this? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    How do you import a plain text file as a single character string in R? I think that this will probably ... is probably unstable too. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    The install.packages() function in R is the automatic unzipping utility that gets and install packages in R. ... accesses packages? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have code that at one place ends up with a list of data frames which I really want to convert ... starting with (this is grossly simplified for illustration): listOfDataFrames...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have a data frame with a column of p-values and I want to make a selection on these p-values. > pvalues_anova ... 7.125751e-01 5.193604e-01 4.835312e-04 Selection way: anovatest...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    Let's say that I have a date in R and it's formatted as follows. date 2012-02-01 2012-02-01 2012-02- ... generate the day by the date. Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    A categorical variable V1 in a data frame D1 can have values represented by the letters from A to Z. I want to create a ... B','N',T')) Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    According to the R language definition, the difference between & and && (correspondingly | and ||) is that the former ... OrElse in R? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
...