gpt4 book ai didi

r - 如何显示趋势线的图例?

转载 作者:行者123 更新时间:2023-12-01 03:18:34 25 4
gpt4 key购买 nike

问题

似乎我很难显示使用 stat_smooth() 生成的趋势线.在我使用参数之前 show.legend = T ,我有一个图形看起来像这样:

IMAGE

添加参数后,我得到了这样的东西:

IMAGE2

但是你看,我想单独显示趋势线图例,像这样:

IMAGE3

我如何实现这一目标?如果您需要,我的源代码在这里(如果您能帮我截断代码以使其更简洁,我将不胜感激):

library(ggplot2)
library(ggrepel)
library(ggthemes)
library(scales)
library(plotly)
library(grid)
library(extrafont)

# read data
econ <- read.csv("https://raw.githubusercontent.com/altaf-ali/ggplot_tutorial/master/data/economist.csv")
target_countries <- c("Russia", "Venezuela", "Iraq", "Myanmar", "Sudan",
"Afghanistan", "Congo", "Greece", "Argentina", "Brazil",
"India", "Italy", "China", "South Africa", "Spane",
"Botswana", "Cape Verde", "Bhutan", "Rwanda", "France",
"United States", "Germany", "Britain", "Barbados", "Norway", "Japan",
"New Zealand", "Singapore")

econ$Country <- as.character(econ$Country)
labeled_countries <- subset(econ, Country %in% target_countries)
vector <- as.numeric(rownames(labeled_countries))

econ$CountryLabel <- econ$Country
econ$CountryLabel[1:173] <- ''
econ$CountryLabel[c(labeled_countries$X)] <- labeled_countries$Country

# Data Visualisation
g <- ggplot(data = econ, aes(CPI, HDI)) +
geom_smooth(se = FALSE, method = 'lm', colour = 'red', fullrange = T, formula = y ~ log(x), show.legend = T) +
geom_point(stroke = 0, color = 'white', size = 3, show.legend = T)

g <- g + geom_point(aes(color = Region), size = 3, pch = 1, stroke = 1.2)

g <- g + theme_economist_white()

g <- g + scale_x_continuous(limits = c(1,10), breaks = 1:10) +
scale_y_continuous(limits = c(0.2, 1.0), breaks = seq(0.2, 1.0, 0.1)) +
labs(title = 'Corruption and human development',
caption='Source: Transparency International; UN Human Development Report')


g <- g + xlab('Corruption Perceptions Index, 2011 (10=least corrupt)') +
ylab('Human Development Index, 2011 (1=best)')

g <- g + theme(plot.title = element_text(family = 'Arial Narrow', size = 14, margin = margin(5, 0, 12, 0)),
plot.caption = element_text(family = 'Arial Narrow', hjust = 0, margin=margin(10,0,0,0)),
axis.title.x = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(10, 0, 10, 0)),
axis.title.y = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(0, 10, 0, 10)),
plot.background = element_rect(fill = 'white'),
legend.title = element_blank()
) + theme(legend.background = element_blank(),
legend.key = element_blank(),
legend.text = element_text(family = 'Arial Narrow', size = 10)) +
guides(colour = guide_legend(nrow = 1))

g <- g + geom_text_repel(data = econ, aes(CPI, HDI, label = CountryLabel), family = 'Arial Narrow',
colour = 'grey10', force = 8, point.padding = 0.5, box.padding = 0.3,
segment.colour = 'grey10'
)

g
grid.rect(x = 1, y = 0.996, hjust = 1, vjust = 0, gp = gpar(fill = '#e5001c', lwd = 0))
grid.rect(x = 0.025, y = 0.91, hjust = 1, vjust = 0, gp = gpar(fill = '#e5001c', lwd = 0))

奖金请求

作为一个审美标准很高的人,我想知道:
  • 如何制作国家/地区标签分割不是 直的?引用第三张图,注意“中国”的分割线不是直的。
  • 如何排列我的国家/地区标签,使其不会在散点和趋势线上重叠? (我咨询了 this Stack Overflow 帖子,从我的代码中可以看出,我为不需要的国家/地区创建了空字符串。但是,重叠仍然存在)
  • 如何将整个情节转换为可以嵌入网站的交互式情节?


  • 编辑:
    感谢@aosmith 提供有用的建议。我关注了 this post并试图 override.aes我的趋势线。这是我添加到 #Data Visualisation 的内容 session :
    g <- ggplot(data=econ, aes(CPI,HDI))+
    geom_smooth(se = FALSE, method = 'lm', aes(group = 1, colour = "Trendline"),fullrange=T, linetype=1,formula=y~log(x))+
    scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta","orange", "red"),
    guides (colour = guide_legend (override.aes = list(linetype = 1)))

    )+
    geom_point(...)
    ...

    值得庆幸的是,它显示了趋势线图例。但仍然不理想:

    enter image description here

    如何改进代码?

    最佳答案

    问题出在 guides陈述。这是代码的数据可视化部分,有些修复:

    # Data Visualisation
    g <- ggplot(data = econ, aes(CPI, HDI)) +
    geom_smooth(se = FALSE, method = 'lm', aes(group = 1, colour = "Trendline"), fullrange=T, linetype=1, formula=y~log(x)) +
    geom_point(stroke = 0, color = 'white', size = 3, show.legend = T) +
    scale_colour_manual(values = c("purple", "green", "blue", "yellow", "magenta", "orange", "red"))


    g <- g + geom_point(aes(color = Region), size = 3, pch = 1, stroke = 1.2)

    g <- g + theme_economist_white()

    g <- g + scale_x_continuous(limits = c(1,10), breaks = 1:10) +
    scale_y_continuous(limits = c(0.2, 1.0), breaks = seq(0.2, 1.0, 0.1)) +
    labs(title = 'Corruption and human development',
    caption='Source: Transparency International; UN Human Development Report')


    g <- g + xlab('Corruption Perceptions Index, 2011 (10=least corrupt)') +
    ylab('Human Development Index, 2011 (1=best)')

    g <- g + theme(plot.title = element_text(family = 'Arial Narrow', size = 14, margin = margin(5, 0, 12, 0)),
    plot.caption = element_text(family = 'Arial Narrow', hjust = 0, margin=margin(10,0,0,0)),
    axis.title.x = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(10, 0, 10, 0)),
    axis.title.y = element_text(family = 'Arial Narrow', face = 'italic', size = 8, margin = margin(0, 10, 0, 10)),
    plot.background = element_rect(fill = 'white'),
    legend.title = element_blank()
    ) + theme(legend.background = element_blank(),
    legend.key = element_blank(),
    legend.text = element_text(family = 'Arial Narrow', size = 10))

    g <- g + geom_text_repel(data = econ, aes(CPI, HDI, label = CountryLabel), family = 'Arial Narrow',
    colour = 'grey10', force = 8, point.padding = 0.5, box.padding = 0.3,
    segment.colour = 'grey10'
    )

    g + guides(colour = guide_legend(nrow = 1,
    override.aes = list(linetype = c(rep("blank", 6), "solid"),
    shape = c(rep(1, 6), NA)
    )
    )
    )

    enter image description here

    关于r - 如何显示趋势线的图例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47559580/

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