-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
italicize text #13
Comments
It is a bit unclear whether the question refers to the legend, axis labels, or another text element in a library(plotbiomes)
#> Happy biome plotting!
library(ggplot2)
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
# Fake data
fake_data <- data.frame(
temp = c(10, 15, 20),
pp_cm = c(100, 200, 150),
taxa = c("Some taxa_1", "Some taxa_2", "Other taxa_3")
)
# Dynamically create formatted labels with the first word italicized
fake_data <- fake_data %>%
mutate(
taxa_label = gsub("(\\w+) (\\w+)", "italic('\\1')~'\\2'", taxa)
)
# Create a plot with automatic legend
whittaker_base_plot() +
geom_point(data = fake_data,
aes(x = temp,
y = pp_cm,
color = taxa_label)) + # Map formatted labels to color
scale_color_manual(
values = setNames(c("blue", "red", "green"), fake_data$taxa_label),
labels = parse(text = fake_data$taxa_label), # Parse dynamic labels
guide = guide_legend(order = 1) # Ensure this legend is on top of the "Whittaker biomes" default legend
) +
labs(color = "Taxa") + # Adjust legend title
theme_minimal() # Dynamically create formatted labels with the second word italicized
fake_data <- fake_data %>%
mutate(
taxa_label = gsub("(\\w+) (\\w+)", "'\\1'~italic('\\2')", taxa)
)
# Create a plot with automatic legend
whittaker_base_plot() +
geom_point(data = fake_data,
aes(x = temp,
y = pp_cm,
color = taxa_label)) + # Map formatted labels to color
scale_color_manual(
values = setNames(c("blue", "red", "green"), fake_data$taxa_label),
labels = parse(text = fake_data$taxa_label), # Parse dynamic labels
guide = guide_legend(order = 1) # Ensure this legend is on top of the "Whittaker biomes" default legend
) +
labs(color = "Taxa") + # Adjust legend title
theme_minimal() Created on 2024-12-14 with reprex v2.1.0 |
Got this email:
The text was updated successfully, but these errors were encountered: