我有一段代码可以在R中生成一个图形,它可以工作,但是有一个字段名
star_sign
variable_name
而是删除硬编码。
my_graph
{
scale_fac
row_count = nrow(df_name)
#problem line here - star_sign appears twice
df_name % mutate(star_sign = as.numeric(factor(star_sign, levels = my_levels)))
df_name %>%
ggplot(aes_string(x = variable_name)) +
geom_line(aes(y = mean), color = "red") +
geom_line(aes(y = min), color = "blue") +
geom_line(aes(y = max), color = "green") +
geom_col(aes(y = exposure / scale_fac), width = 0.5, fill = "blue") +
scale_y_continuous("Linear Predictor", sec.axis = sec_axis(~ .*scale_fac , name = "Exposure")) +
scale_x_continuous(variable_name, breaks = c(1:row_count), labels = df_name$my_levels) +
theme_bw()
}
df
exposure = c(50, 70, 60, 40, 45, 78, 42, 22, 28, 49, 50, 31),
mean = c(1.1, 1.2, 1.4, 1.3, 1.8, 1.6, 1.4, 1.3, 1.2, 1.1, 1.5, 1.3))
df$min
df$max
df$my_levels = c("Aries", "Taurus", "Gemini", "Cancer", "Leo", "Virgo", "Libra", "Scorpio", "Sagittarius", "Capricorn", "Aquarius", "Pisces")
my_graph(df, "star_sign")
硬编码在这一行
df_name % mutate(star_sign = as.numeric(factor(star_sign, levels = my_levels)))
谢谢您。