[英]how to arrange a table of a variable in ascending order in R and draw a bar-chart
我僅使用1個變量(DX_2_CD)從數據框(編號.out)創建了一個表。
> counts <- table(no.out$DX_2_CD)
> counts
Blood CirculatorySystems Congenital Digestive Genitourinary
7 133 0 7 35
Illdefined Immunity Infectious Injury Mental
126 98 0 84 7
Muscular Neoplasms Nervous Perinatal Pregnancy
119 7 0 0 7
Respiratory Sense Skin
63 35 63
現在,我想按升序重新排列計數,以使最低的值為第一個,最高的值為最后一個。 如果有兩個相同的值,則先哪個都無所謂。
我認為最簡單的解決方案是使用order():
ordered_counts <- counts[order(counts)]
甚至更好的是sort():
ordered_counts <- sort(counts)
好的,現在我設法使用以下方法獲得了不同顏色的條:
x<-barplot(sort(counts),col=rainbow(length(sort(counts))))#, main="DX_2_CD Distribution",
lablist <- as.vector(names(sort(counts)))
text(cex=1, x=x-.50, y=-1.25, lablist, xpd=TRUE, srt=45)
這解決了兩個問題。
但是,除了45度傾斜的標簽外,原始的x軸標簽也保留了下來。 我如何擺脫它們?
我沒有足夠的聲譽,因此無法附加屏幕截圖
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.