我有這兩列,即水果和頻率在我的dataframe.I試圖繪製直方圖或條形圖,使我的x軸有水果,y有freq.My表是喜歡這個。ggplot在數據框中的兩列
fruit freq
apple 5
orange 0
banana 3
grapes 10
我是初學者,無法弄清楚這一點。
我有這兩列,即水果和頻率在我的dataframe.I試圖繪製直方圖或條形圖,使我的x軸有水果,y有freq.My表是喜歡這個。ggplot在數據框中的兩列
fruit freq
apple 5
orange 0
banana 3
grapes 10
我是初學者,無法弄清楚這一點。
這裏是你可以做什麼:
# Define the data
myData = data.frame("fruit" = c("apple", "orange", "banana", "grapes"), "freq" = c(5, 0, 3, 10))
# Load library
library(ggplot2)
# Call plot function
ggplot(data = myData, aes(x = fruit, y = freq)) + geom_bar(stat = "identity")
我們稱之爲ggplot()函數來創建一個基本的/空積層。在這個函數中,我們告訴ggplot我們的數據是什麼,並告訴它映射到'x'和'y'。你的'x'是水果,'y'是freq。之後我們通過調用geom_bar(stat = "identity")
來添加條形圖。我們在這裏使用stat = "identity"
,因爲我們已經計算了每種水果。更多信息在這裏給出:http://docs.ggplot2.org/0.9.3.1/geom_bar.html。
謝謝你.. – richa1465
三江源這麼多..我明白了.. – richa1465