首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在ggplot2 + ggfortify中添加图例的问题

在ggplot2 + ggfortify中添加图例的问题
EN

Stack Overflow用户
提问于 2017-11-19 15:32:14
回答 2查看 591关注 0票数 2

我很难用

代码语言:javascript
复制
scale_colour_manual 

图形的功能。我试过了

代码语言:javascript
复制
guide = "legend" 

强制传说出现了,但不起作用。代表代码:

代码语言:javascript
复制
library(ggfortify)
library(ggplot2)
p  <- ggdistribution(pgamma, seq(0, 100, 0.1), shape = 0.92, scale = 22, 
                     colour = 'red')
p2 <- ggdistribution(pgamma, seq(0, 100, 0.1), shape = 0.9, scale = 5, 
                     colour = 'blue', p=p)

p2 + 
theme_bw(base_size = 14) +
theme(legend.position ="top") +
xlab("Precipitación") +
ylab("F(x)") +
scale_colour_manual("Legend title", guide = "legend", 
                      values = c("red", "blue"), labels = c("Observado","Reforecast")) +
ggtitle("Ajuste Gamma")

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-19 16:15:17

一种基于stat_function的解决方案

代码语言:javascript
复制
library(ggplot2)
library(scales)

cols <- c("LINE1"="red","LINE2"="blue")
df <- data.frame(x=seq(0, 100, 0.1))
ggplot(data=df, aes(x=x)) + 
stat_function(aes(colour = "LINE1"), fun=pgamma, args=list(shape = 0.92, scale = 22)) +
stat_function(aes(colour = "LINE2"), fun=pgamma, args=list(shape = 0.9, scale = 5)) +
theme_bw(base_size = 14) +
theme(legend.position ="top") +
xlab("Precipitación") +
ylab("F(x)") +
scale_colour_manual("Legend title", values=c(LINE1="red",LINE2="blue"),
                    labels = c("Observado","Reforecast")) +
scale_y_continuous(labels=percent) +
ggtitle("Ajuste Gamma")

票数 1
EN

Stack Overflow用户

发布于 2020-11-08 15:47:09

这似乎是ggfortify.*的一个bug,您只需使用来自ggplot2geom_line()就可以获得相同的结果:

代码语言:javascript
复制
library(ggplot2)

# Sequence of values to draw from dist(s) for plotting
x = seq(0, 100, 0.1)

# Defining dists
d1 = pgamma(x, shape=0.92, scale=22)
d2 = pgamma(x, shape=0.90, scale=5)

# Plotting
p1 = ggplot() +
     geom_line(aes(x,d1,colour='red')) + 
     geom_line(aes(x,d2,colour='blue')) + 
     
     theme_bw(base_size = 14) +
     theme(legend.position="top") +

     ggtitle("Ajuste Gamma") +
     xlab("Precipitación") +
     ylab("F(x)") +
     
     scale_colour_manual("Legend title", 
                         guide = "legend", 
                         values = c("red", "blue"), 
                         labels=c("Observado", "Reforecast"))

*相关问题:Plotting multiple density distributions on one plot

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47378577

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档