简体   繁体   中英

How to define axis labels for ggplots elsewhere in code?

As my variables are coded as p1, p2, p3 etc, I need to plot them using their actual meaningful names.

How to advance my code to do that? I tried something like this but did not work.

p1_title = "Smoking rate"
p2_title = "Traffic injury rate"

data %>% ggplot(aes(p1, outcomevariable))+
geom_line()+
xlab(p1_title)

data %>% ggplot(aes(p2, outcomevariable))+
geom_line()+
xlab(p2_title)

look at the code, should be done it: Source: https://www.datanovia.com/en/blog/ggplot-axis-labels/#change-axis-labels

library(ggplot2)
p <- ggplot(ToothGrowth, aes(x = factor(dose), y = len)) + 
  geom_boxplot()

# Default plot
print(p)

# Change axis labels
p <- p + labs(x = "Dose (mg)", y = "Teeth length")
p

KR

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM