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.?
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.
You can use the following syntax to change the legend labels in ggplot2: p + scale_fill_discrete(labels=c('label1', 'label2', 'label3', ...))
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.
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
#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.
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).
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:
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()
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:
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With