2016-08-24 66 views
0

一个ggplot我试图创建基于此数据ggplot:创建r中

enter image description here

很抱歉,但我不能提供更多的从数据。

我从效果包的效果输出中得到了这个。

现在我想绘制一个ggplot,它将prob.X1到prob.X6的概率组合在一起。它应该是这样的:

enter image description here

这就是我已经尝试过:

Eff.1 <- Effect(focal.predictors = "AvRating", mod= V6) 
Eff.df <- data.frame((Eff.1)) 

此代码从上面提供的图表。

有了这个,我试图让ggplot:

ggplot(Eff.df) + geom_area(aes(x=AvRating, y=prob.X1)) 

但这只是创建情节在第一和第二列。

我尝试添加更多geoms这样的(即不工作):

ggplot(Eff.df) + geom_area(aes(x=AvRating, y=prob.X1)) + geom_area(aes(x=AvRating, y=prob.X2)) 

比我试图连接列和绘制这个(不工作):

Eff.dfx <- as.numeric((rbind(Eff.df$prob.X1,Eff.df$prob.X2, Eff.df$prob.X3, Eff.df$prob.X4,Eff.df$prob.X5,Eff.df$prob.X6))) 
Eff.dfAv <- as.numeric(rep(Eff.df$AvRating,6)) 
ggplot(Eff.df) + geom_area(aes(x=Eff.dfAv, y= Eff.dfx)) 

你能帮我一下这个ggplot的代码吗?

非常感谢。

+2

请提供您正在使用的数据。 'dput()'应该可以帮助你。还提供一些你已经试过的代码。编辑你的问题。 – loki

+0

可能您可以先看看@ [ggplot2上的SO文档](http://stackoverflow.com/documentation/r/1334/ggplot2#t=201608241415466235163)或[R Graphics Cookbook](http:// www .cookbook -r.com/Graphs /) – loki

+0

啊。非常感谢。此链接有所帮助。 – heino06

回答

0

如果您真的提供MWE或人们可以用来真正帮助您的数据,它总是有帮助的。既然你没有,这里有一些数据已经以你可能需要的格式存在了。您将需要reshape2::melt将您的数据(当前是一张图片,这比张贴数据更简单)转换为正确的格式。

你可能不应该使用的收视率之间的联系,因为这意味着你中途有他们

enter image description here

之间

library(dplyr) 

usableData <- 
    data.frame(
    avgRating = factor(rep(0:5, each = 6)) 
    , value = factor(rep(1:6, 6)) 
    , count = rnorm(36, rep(seq(300,40, length.out = 6)), 10) 
) %>% 
    group_by(avgRating) %>% 
    mutate(prob = count/sum(count)) 


ggplot(usableData 
     , aes(x = avgRating 
      , y = prob 
      , fill = value)) + 
    geom_bar(stat = "identity") + 
    theme_minimal() + 
    scale_fill_brewer(direction = -1) 
关于价值的信息。如果你真的有一个很好的理由使用连接线,你应该能够做这样的事情:

usableData %>% 
    ungroup() %>% 
    group_by(avgRating) %>% 
    mutate(cumsum = cumsum(count) 
     , cumProb = cumsum/sum(count) 
     , cumProbPre = c(0,cumsum[-length(cumsum)])/sum(count) 
     ) %>% 
    ggplot(aes(x = as.numeric(avgRating) 
      , ymin = cumProbPre 
      , ymax = cumProb 
      , fill = value 
      )) + 
    geom_ribbon() + 
    theme_minimal() + 
    scale_fill_brewer(direction = -1) 

enter image description here

+0

感谢您的回答。你真的帮助了我。 对不起这个没用的图片。我第一次这样做,并没有意识到如何插入数据,以便您可以使用它们。 – heino06