2

我在使用时遇到问题

scale_colour_manual 

ggplot的功能。我试过

guide = "legend" 

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

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")

在此处输入图像描述

4

2 回答 2

1

一个解决方案stat_function

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")

在此处输入图像描述

于 2017-11-19T16:15:17.007 回答
0

这似乎是ggfortify.* 的一个错误,但您只需使用geom_line()from即可获得相同的结果ggplot2

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"))

结果图


* 相关问题:在一个地块上绘制多个密度分布

于 2020-11-08T15:47:09.983 回答