2016-08-24 68 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