gpt4 book ai didi

r - ggplot 顶部带有标题的水平图例

转载 作者:行者123 更新时间:2023-12-04 11:19:06 25 4
gpt4 key购买 nike

我试图将图例的标题放在顶部,而值是水平分布的,但我不能。任何提示将不胜感激。

下面的代码提供了下面的图表,但我的图表上没有空间,所以我需要这样的东西:

性别

女性男性

df1 <- data.frame(
sex = factor(c("Female","Female","Male","Male")),
time = factor(c("Lunch","Dinner","Lunch","Dinner"), levels=c("Lunch","Dinner")),
total_bill = c(13.53, 16.81, 16.24, 17.42))

lp1 <- ggplot(data=df1,
aes(x=time, y=total_bill, group=sex, shape=sex, colour=sex)) +
geom_line() +
geom_point() +
theme_bw() +
theme(
legend.direction = "horizontal",
) +
scale_color_manual(values=c("#0000CC", "#CC0000"),
name = 'Gender')
lp1

enter image description here

最佳答案

尝试这个:

cols <- c("#0000CC", "#CC0000")

df1 %>%
ggplot(aes(time, total_bill, group = sex, shape = sex, colour = sex)) +
geom_line() +
geom_point() +
theme_bw() +
scale_shape(
guide = guide_legend(
direction = "horizontal",
title.position = "top"
)
) +
scale_color_manual(
values = cols,
name = "Gender",
guide = guide_legend(
direction = "horizontal",
title.position = "top"
)
)

enter image description here

关于r - ggplot 顶部带有标题的水平图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47127253/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com