0
我試圖在R中訂購條形圖,但是我收到了有趣的結果。數據源是一個列表。也許這是問題,但我不知道如何解決。訂購R中的條形圖作爲數據源列表
我用下面的代碼獲得它:
file <- c("C://path//file.csv")
dataSource <- read.table(file, header=T, sep=",")
#typeof(dataSource) ==> this gives me a 'list'
dataChart <- dataSource[ , c("Product","YTD")]
dataChart <- na.omit(dataChart)
barplot(dataChart[ ,2], names.arg = dataChart[ ,1],
horiz = TRUE,
ylab = "",
xlab = "",
main="Title",
las=1)
如果我添加以下圖表進行排序:
dataChart <- as.data.frame(dataChart)[order("YTD"),]
我得到以下內容:
數據源如下:
dataSource = structure(list(Product = structure(c(2L, 4L, 5L, 7L, 8L, 9L,
10L, 13L, 1L, 3L, 4L, 6L, 11L, 12L, 13L), .Label = c("ADT", "BHL",
"CEX", "CMX", "CTL", "HTH", "MTL", "SSL", "TLS", "UTV", "WEX",
"WLD", "WMX"), class = "factor"), YTD = c(67L, 5026L, 503L, 29L,
3975L, 876L, NA, 40L, 62L, 1395L, 717L, NA, 423L, 277L, 35L)), .Names = c("Product",
"YTD"), class = "data.frame", row.names = c(NA, -15L))
如何讓圖表排序正確任何想法?問題是如何加載數據的問題?
結構(列表(產品=結構(C(2L,4L,5L,7L,8L,9L, 10L,13L,1L,3L,4L,6L,11L,12L, 13L).Label = c(「ADT」,「BHL」, 「CEX」,「CMX」,「CTL」,「HTH」,「MTL」,「SSL」,「TLS」,「UTV」 WEX「, 」WLD「,」WMX「),class =」factor「),YTD = c(67L,5026L,503L,29L, 3975L,876L,NA,40L,62L,1395L,717L,NA,423L ,277L,35L)),.Names = c(「Product」, 「YTD」),class =「data.frame」,row.names = c(NA,-15L)) – Selrac