How to change legend label in "theme" argument in ggplot2?

Kacey

I am using ggplot2 ro draw error bar plot. For the legend, I want it like this:

  1. inside the plot. I know function theme(legend.position) can do this.

  2. use latex symbol in the legend labels with function expression.

I have read several references of constructing the legend, but still have not found what I want exactly. I can perform either 1 or 2, but can not do both in my plot.

Following is R code of the plot with the problem. Since my reputation is not enough to post an image, please copy the code into your R to see the plot. I only want to keep the label contains \beta_A and \beta_B, but not the label contains group 1 and group 2.

Any ideas? Thank you!

library(ggplot2); library(scales) #for alpha
varx <- rep(c("group1", "group2"), each = 3)
vary <- rep(c("A", "B", "C"), 2)
poi <- sample(20:30, 6)
upper <- sample(40:50, 6)
lower <- sample(1:10, 6)

dat <- data.frame(varx, vary, poi, upper, lower)
dat
  #      varx vary poi upper lower
  # 1 group1    A  29    42    10
  # 2 group1    B  21    48     9
  # 3 group1    C  26    47     8
  # 4 group2    A  30    44     4
  # 5 group2    B  27    49     6
  # 6 group2    C  24    43     7

pp <- ggplot(dat, aes(colour = varx, y = poi, x = vary)) 

limits <- aes(ymax = upper, ymin = lower)

pp  + geom_point(aes(shape=varx), position = position_dodge(0.3), size = 2) + 
  ## dodge make the lines not stack upon each other
  geom_errorbar(limits, size = 1, width = 0.15, position = position_dodge(0.3)) + 
  theme_bw() + ## Get rid of the grey background
  geom_hline(yintercept = 0, linetype = "dotdash") +
  coord_cartesian(ylim = c(1, 60)) + 
  scale_shape_manual(values = c(17, 19, 18)) + 
  scale_colour_hue(labels =  c(expression(beta[A]), expression(beta[N]))) + 
  theme(plot.margin = unit(rep(1.5, 4),"mm"),
        legend.justification= c(1, 0), 
        legend.position = c(1, 0.5),
        legend.key = element_blank(),## Get rid of the legend box 
        legend.title = element_blank(),
        legend.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(fill=alpha(0.0001))) +
  labs(x = NULL, y = NULL)
Rorschach

To remove a legend you can simply add guides(shape=FALSE) to your plot. In guides you can decide which aesthetics should display legends. To combine legends, make the labels the same and ggplot will do the rest.

## Use this label for both aesthetic legends
labels =  c(expression(beta[A]), expression(beta[N]))

pp  + geom_point(aes(shape=varx), position = position_dodge(0.3), size = 2) + 
  ## dodge make the lines not stack upon each other
  geom_errorbar(limits, size = 1, width = 0.15, position = position_dodge(0.3)) + 
  theme_bw() + ## Get rid of the grey background
  geom_hline(yintercept = 0, linetype = "dotdash") +
  coord_cartesian(ylim = c(1, 60)) + 
  scale_shape_manual(values = c(17, 19, 18), labels=labels) +
  scale_colour_hue(labels = labels) + 
  theme(plot.margin = unit(rep(1.5, 4),"mm"),
        legend.justification= c(1, 0), 
        legend.position = c(1, 0.5),
        legend.key = element_blank(),## Get rid of the legend box 
        legend.title = element_blank(),
        legend.text = element_text(size = 10, face = "bold"),
        legend.background = element_rect(fill=alpha(0.0001))) +
  labs(x = NULL, y = NULL) # + guides(shape=FALSE) # if wanting to remove

enter image description here

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to change legend label in "theme" argument in ggplot2?

From Dev

How to add a label for a vertical line with legend in ggplot2

From Dev

How can I change the legend geometry in ggplot2

From Dev

How to change the color legend in fine granularity using ggplot2

From Dev

How to change angle of line in customized legend in ggplot2

From Dev

How to change background colour of legend in ggplot2?

From Dev

How to change the size of legend text in ggplot2?

From Dev

How to remove grey borders around individual entries in ggplot2 legend when using theme_bw?

From Dev

How to change the size of individual legend label size?

From Dev

How to include Greek/plotmath characters for one label in legend for ggplot2

From Dev

ggplot2: Change factor order in legend

From Dev

Cannot change legend in ggplot2

From Dev

ggplot2: Change legend symbol

From Dev

Change the symbol in a legend key in ggplot2

From Dev

change labels in legend for ggplot2

From Dev

ggplot2: Change legend symbol

From Dev

change labels in legend for ggplot2

From Java

How to change legend title in ggplot

From Dev

How to change legend in shape of ggplot

From Dev

ggplot2 change colorbar label

From Dev

`ggplot2` legend not showing label for added series

From Dev

How to reorder a legend in ggplot2?

From Dev

ggplot2: missing legend and how to add?

From Dev

How to transpose legend in ggplot2

From Dev

How to put variables in legend in ggplot2

From Dev

Change order of items in ggplot2 legend with combined items

From Dev

Is it possible to change the legend size in base R (no ggplot2)

From Dev

ggplot2: Change color of background for geom_vline legend

From Java

How do I change the title of the legend in this ggplot

Related Related

HotTag

Archive