2017-05-28 31 views
0

我試圖做一個情節,顯示三明治店不同類型的三明治的卡路里計數。即多少卡路里有在地鐵一個蔬菜三明治VS吉米·約翰等遞增地在R建築一個barplot

我想想象這是一個barplot,那

  • 對每個三明治的卡路里酒吧

  • 明顯組別三明治按類型蔬菜,烤牛肉等,其中每個組根據不同的v endors。

我的數據看起來像這樣(編輯的重現性):

cleaninput <- data.frame ("type" = c("italian", "turkey", "roastbeef", "club", "veggie", "italian", "turkey", "roastbeef", "veggie"), 
"vendor" = c("subway", "subway", "subway", "subway", "subway", "jimmyjohns", "jimmyjohns", "jimmyjohns", "jimmyjohns"), 
"calories" = c(410,280,320,310,230,640,510,540,690)) 

我嘗試遍歷像這樣的數據,其中cleaninput是我data.frame

#set up barplot 
barplot(height = mean(cleaninput[['calories']])) 
#iterate over sandwich types 
for (t in levels(cleaninput[['type']])) 
{ 
    cat(t,"\n") 

    barplot(cleaninput[cleaninput[['type']]==t,][['calories']], add = TRUE) 
} 

的想法首先要設置barplot,然後迭代地爲每個三明治類型添加條。我understoodadd設置要做到這一點。我使用linespoints命令完成了類似的事情,並重現了下面的例子 - 這就是我想要轉換成barplot的情況。

enter image description here

但是,這是行不通的,因爲它似乎玉米粥對方(CF輸出下同)的頂部所有的酒吧。

我的問題

  • (如何)可以解決這個問題?最好我想使用base R而不是ggplot來使它更​​便於攜帶。

  • 有沒有比for -loop更好的方法?

我看着tutorials分組的barline,但沒有看到他們是如何翻譯我的問題。

電流輸出:

ugly R plot

+0

請提供數據或它的一個子集以可用的形式 - 我們可以複製和粘貼,而無需格式化例如通過粘貼'dput(cleaninput)'的輸出。你可以考慮學習ggplot2。 http://www.sthda.com/english/wiki/ggplot2-barplots-quick-start-guide-r-software-and-data-visualization – Djork

+0

@ R.S。謝謝,我認爲我的例子是複製和粘貼的,但它不是,我用一個正確的替換了它。感謝ggplot的建議,但正如我上面所說,我希望得到這個基地的工作。 – patrick

回答

1

這裏是壘積的解決方案的要求,因爲這是你的喜好了ggpltot2。

第一步是將您的數據以大型格式用於基本繪圖,例如:通過使用reshape2::dcasttidyr::spread

library(tidyr) 
library(tidyverse) 
cleaninput_spread <- cleaninput %>% spread(type, calories) %>% remove_rownames %>% column_to_rownames(var="vendor") 
cleaninput_spread 

>   club italian roastbeef turkey veggie 
> jimmyjohns NA  640  540 510 690 
> subway  310  410  320 280 230 

替換NA值與0:

cleaninput_spread[is.na(cleaninput_spread)] <- 0 

在鹼疊放barplot:

barplot(as.matrix(cleaninput_spread), main="Calories per Sandwich, by shop", 
     xlab="Sandwich", ylab="Calories", 
     col=c("darkblue","red"), 
     legend = rownames(cleaninput_spread)) 

enter image description here

+0

謝謝先生,這非常有幫助!如果您不介意,我會進行一次快速跟進:我時常看到這個'%>%'操作符並且理解它是一個類似於管道的命令;是否正確讀取代碼,我可以在單個命令中執行所有這些操作,'%>%'主要是爲了方便和可讀性?再次感謝劇情! – patrick

+1

是的,這相當於'column_to_rownames(remove_rownames(spread(cleaninput,type,calories)),var =「vendor」)',並且可以根據需要爲每個嵌套函數分配變量。 – Djork

1

這是你在找什麼?

type<-c("italian","turkey","roastbeef","club","veggie","italian","turkey","roastbeef","veggie") 
vendor<-c(rep("subway",5),rep("jimmyjohns",4)) 
calories<-c(410,280,320,310,230,640,510,540,690) 
size<-c(rep(6,5),rep(8,4)) 

cleaninput<-data.frame(type,vendor,calories,size) 

#first you calculate the mean by type using by function (base package) 

calor.by.type<-by(cleaninput$calories,INDICES = list(cleaninput$type),FUN = mean) 

#then you plot the result from by function 

barplot(calor.by.type,main="by function") 

enter image description here

+0

感謝您的幫助!不幸的是,這不是我想要做的 - 我想按供應商分類每種類型,即地鐵的「俱樂部」與jimmyjohns「俱樂部」等。我已經爲我的問題添加了預期的輸出示例並試圖澄清措辭......對不起,如果我不清楚。 – patrick