Skip to content
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

Open
valentinitnelav opened this issue Dec 14, 2024 · 1 comment
Open

italicize text #13

valentinitnelav opened this issue Dec 14, 2024 · 1 comment

Comments

@valentinitnelav
Copy link
Owner

Got this email:

I am using your plot_bipartite() function to make some plots and I was wondering if you know of a way to partially italicize text on one side of the plot so that species epithet abbreviations can be plain text while the genus names are italicized?

@valentinitnelav
Copy link
Owner Author

It is a bit unclear whether the question refers to the legend, axis labels, or another text element in a ggplot2 plot. Below is an example demonstrating how to add an additional legend for taxa.
I created some sample data containing text where either the first or second word can be italicized, as shown in two examples: one with the first word italicized and another with the second word italicized.

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant