R语言 我怎样才能使我的情节看起来像在右边?

kxkpmulp  于 2023-05-20  发布在  其他
关注(0)|答案(1)|浏览(106)

我想画一些图,这是我的代码

ggplot(data.frame(x=c(0,25)),aes(x=x))+
stat_function(fun=function(x){(18-6*x)/4},aes(color="Function 1"))+
stat_function(fun=function(x){(24-4*x)/3},aes(color="Function 2"))+
stat_function(fun=function(x){(20-10*x)/2},aes(color="Function 3"))+
theme_bw()+
scale_color_discrete(name="Function")+
geom_polygon(data=data.frame(x=c(0,3,Inf,3),y=c(4.5,0,Inf,Inf)),
             aes(x=x,y=y,fill="Constraint 1"),inherit.aes=F,alpha=0.4)+
scale_fill_discrete(name="Constraint Set")+
scale_y_continuous(limits=c(0,10.5))+
scale_x_continuous(limits=c(0,7))

我得到的结果是..


这是我想要的输出。

42fyovps

42fyovps1#

从图片上很难准确地知道你想要阴影的区域。看起来好像您希望功能1上方的区域着色。这样的话

ggplot(data.frame(x = c(0,25)), aes(x = x)) +
  stat_function(fun = ~(18 - 6 * .x)/4, aes(color = "Function 1")) +
  stat_function(fun = ~(24 - 4 * .x)/3, aes(color = "Function 2")) +
  stat_function(fun = ~(20 - 10 * .x)/2, aes(color = "Function 3")) +
  theme_bw() +
  scale_color_discrete(name = "Function") +
  geom_polygon(data = data.frame(x = c(0, 0, 3, Inf, Inf, 0),
                                 y = c(Inf, 4.5, 0, 0, Inf, Inf)),
               aes(x = x, y = y, fill = "Constraint 1"),
               inherit.aes = FALSE, alpha = 0.4) +
  scale_fill_discrete(name = "Constraint Set") +
  scale_y_continuous(limits = c(0, 10.5)) +
  scale_x_continuous(limits = c(0, 7))

如果你想让阴影区域在函数2和函数3这两行的上方,那么它是

ggplot(data.frame(x = c(0,25)), aes(x = x)) +
  stat_function(fun = ~(18 - 6 * .x)/4, aes(color = "Function 1")) +
  stat_function(fun = ~(24 - 4 * .x)/3, aes(color = "Function 2")) +
  stat_function(fun = ~(20 - 10 * .x)/2, aes(color = "Function 3")) +
  theme_bw() +
  scale_color_discrete(name = "Function") +
  geom_polygon(data = data.frame(x = c(0, 0, 6/11, 6, Inf, Inf, 0),
                                 y = c(Inf, 10, (24 - 4 * (6/11))/3,
                                       0, 0, Inf, Inf)),
               aes(x = x, y = y, fill = "Constraint 1"),
               inherit.aes = FALSE, alpha = 0.4) +
  scale_fill_discrete(name = "Constraint Set") +
  scale_y_continuous(limits = c(0, 10.5)) +
  scale_x_continuous(limits = c(0, 7))

用简单的代数就能找到。

相关问题