Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to increase the width of underline drawed in legend labels in ggplot?

Tags:

r

ggplot2

This is my ggplot chart:

ggplot(mtcars) + geom_point(aes(cyl,mpg, color = cyl)) + scale_color_continuous(labels = c(expression(underline("Above 65 & over")),
                  expression(underline("45 - 64")),expression(underline("25 - 44")), expression(underline("15 - 24")),expression(underline("Under 15"))))

First: I want to increase the width of my underline. How can I do this? Also the end of this underline can be an arrow? Is there any package that helps to do this?

Second, I would like to know how to find out the arguments that expression functions can accepts.?

like image 919
Laura Avatar asked Jul 12 '21 22:07

Laura


People also ask

How do you make the legend dots bigger in ggplot2?

To change the Size of Legend, we have to add guides() and guide_legend() functions to the geom_point() function. Inside guides() function, we take parameter color, which calls guide_legend() guide function as value. Inside guide_legend() function, we take an argument called override.

How to change the legend labels in ggplot2?

You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))

How do you underline a title in R?

If you really want to underline text you can use <span style="text-decoration:underline">underline this text</span> for HTML output or $\text{\underline{This sentence underlined using \LaTeX}}$ for pdf output.

How do I change the size of the legend in ggplot2?

Change ggplot2 Legend Title Font Size We can use the legend.title argument to make the legend title font size larger: ggplot (df, aes(fill=position, y=points, x=team)) + geom_bar (position='dodge', stat='identity') + theme (legend.title = element_text (size=30)) Change ggplot2 Legend Text Font Size

How to create grouped boxplots with Custom Legend labels in ggplot?

#create grouped boxplots with custom legend labels p <- ggplot (data, aes(x=team, y=values, fill=program)) + geom_boxplot () + scale_fill_discrete (labels=c ('High Program', 'Low Program')) #display grouped boxplots p The legend now displays the labels that we specified.

How to reverse the Order of the legend items in ggplot2?

The easy way to reverse the order of legend items is to use the ggplot2 legend guides () function. It change the legend order for the specified aesthetic (fill, color, linetype, shape, size, etc).

How to make the legend title font size larger in JavaScript?

We can also use the legend.key.width and legend.key.height arguments to specify widths and heights for the keys: We can use the legend.title argument to make the legend title font size larger:


2 Answers

The only solution I can think of is to draw in the lines "manually" with grid, e.g.

library(tidyverse)
library(grid)

png(filename = "example.png", width = 480, height = 480)
ggplot(mtcars) + geom_point(aes(cyl,mpg, color = cyl)) +
  scale_color_continuous(labels = c(expression(underline(" Above 65 & over")),
                                    expression(underline("45 - 64")),
                                    expression(underline("25 - 44")),
                                    expression(underline("15 - 24")),
                                    expression(underline("Under 15"))))
grid.lines(x = c(0.89, 0.98), y = 0.592,
           arrow = arrow(length = unit(1.5, "mm"), ends = "first"))
grid.lines(x = c(0.91, 0.98), y = 0.547,
           arrow = arrow(length = unit(1.5, "mm"), ends = "first"))
grid.lines(x = c(0.91, 0.98), y = 0.502,
           arrow = arrow(length = unit(1.5, "mm"), ends = "first"))
grid.lines(x = c(0.91, 0.98), y = 0.457,
           arrow = arrow(length = unit(1.5, "mm"), ends = "first"))
grid.lines(x = c(0.831, 0.98), y = 0.414,
           arrow = arrow(length = unit(1.5, "mm"), ends = "first"))
grid.gedit("GRID.line", gp = gpar(lwd = 2))
dev.off()

example.png

Created on 2021-07-16 by the reprex package (v2.0.0)

This approach has the advantages and disadvantages, e.g. you can make the lines look exactly how you want them to look, but you have to specify exactly where on the plot they will be drawn and you can't rescale the plot dynamically (i.e. you need to specify the dimensions of the final plot before you draw the lines). I would be very interested to find out if there is a better way of solving this problem.

For more info on grid graphics:

  • https://bookdown.org/rdpeng/RProgDA/the-grid-package.html
  • https://stat.ethz.ch/R-manual/R-devel/library/grid/doc/index.html
  • The R Graphics book by Paul Murrell
like image 112
jared_mamrot Avatar answered Oct 25 '22 14:10

jared_mamrot


Copied from this thread - which seems to solve at least the width problem.

library(tidyverse)
ggplot(mtcars) +
  geom_point(aes(cyl,mpg, color = cyl)) + 
  scale_color_continuous(labels = c(expression(paste(underline(underline(underline("Above 65 & over"))))),
                                    expression(underline("45 - 64")),
                                    expression(underline("25 - 44")),
                                    expression(underline("15 - 24")),
                                    expression(underline("Under 15"))))

Created on 2021-07-21 by the reprex package (v2.0.0)

like image 1
tjebo Avatar answered Oct 25 '22 15:10

tjebo