2017-06-28 39 views
0

該表顯示了第一行包含12個月的名稱和訪問者的值,葡萄牙語(葡萄牙)和外國人(ESTRANGEIRO)(忽略沒有名字的行)繪製12個月期間的兩個子變量 - R

enter image description here

我怎樣才能情節,GGPLOT2,條形圖,顯示在12個月內,葡萄牙遊客和外國人訪客?

回答

0

通常最好提供一些可重現的代碼示例,而不是提交屏幕截圖,這裏:Click

要完成你想要做的事情,你將不得不改變你的格式。由於看起來像你這樣的數據幀,並使用reshape2

df <- data.frame(month=factor(c("Jan","Feb","Mar"),labels=c("Jan","Feb","Mar"),ordered=TRUE), 
       portugal=c(4000,2330,3000), 
       foreigner=c(4999,2600,3244), 
       stringsAsFactors = FALSE) 


library(reshape2) 
plotdf<-melt(df) 
colnames(plotdf)<-c("Month","Country","Visitors") 
levels(plotdf$Country)<-c("Portgual","Foreigners") 

ggplot(plotdf,aes(x=Month,y=Visitors,fill=Country)) + 
    geom_bar(stat="identity",position=position_dodge()) + 
    xlab("Month") + 
    ylab("Visitors") 

Plot