Muhuri, Pradip (SAMHSA/CBHSQ)
2011-Jul-14 22:24 UTC
[R] Bar chart in ascending order for each level of X
Hello List,
The question is how to plot a bar chart in which bars are sorted in ascending
order for each level of X. I would appreciate receiving your advice and help.
Thanks,
Pradip Muhuri
******
The following codes work when producing the chart in which bars are NOT sorted.
Please see the output.
***** Data File
5.1 8.7 1.6
3.7 7.4 2.8
10.4 12.0 3.5
4.4 8.8 1.7
2.0 3.5 0.7
6.7 11.0 3.1
5.3 6.7 1.8
###############
#source("C:/Documents and Settings/pradip.muhuri/My
Documents/disorders_chart1.R") - Please ignore this line
#R Scripts for bar chart begin here
# Read drug data from tab-delimited data set
drug_data <- read.table("C:/Documents and Settings/pradip.muhuri/My
Documents/xdrug.dat", header=FALSE,
col.names=c("Age_1217", "Age_1825",
"Age_26Plus"),
row.names = c("White","Black","Native
American/Alaska Native","Hawaiian/OPI","Asian",
"More than One Race", "Hispanic"),
sep="\t")
# Graph drug use disorder data with adjacent bars using rainbow colors
barplot(as.matrix(drug_data), main="Past-Year Illicit Drug Use Disorders by
Race/Ethnicity", ylab= "Past-Year Use Disorder Rate (%)",
beside=TRUE, col=rainbow(7))
legend("topright", c("White","Black","Native
American/Alaska Native","Hawaiian/OPI","Asian",
"More than One Race", "Hispanic"), cex=0.6,
bty="n", fill=rainbow(7));
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Bar_Graph.pdf
Type: application/pdf
Size: 4864 bytes
Desc: Bar_Graph.pdf
URL:
<https://stat.ethz.ch/pipermail/r-help/attachments/20110714/19065068/attachment.pdf>
The question is how to plot a bar chart in which bars are sorted in
ascending order for each level of X. I would appreciate receiving your
advice and help.
This toy example might help:
barlab <- sample(LETTERS,10)
values <- sample(1:100, 10)
op <- par(mfrow= c(4,1))
barplot(values, names.arg = barlab)
barplot(values[order(values)], names.arg = barlab[order(values)])
barplot(values[order(barlab)], names.arg = barlab[order(barlab)])
ordlabel <- c(1,3,5,7,9,2,4,6,8,10)
barplot(values[ordlabel], names.arg = barlab[ordlabel])
par(op)
Rob
Thanks,
Pradip Muhuri
******
The following codes work when producing the chart in which bars are NOT
sorted. Please see the output.
***** Data File
5.1 8.7 1.6
3.7 7.4 2.8
10.4 12.0 3.5
4.4 8.8 1.7
2.0 3.5 0.7
6.7 11.0 3.1
5.3 6.7 1.8
###############
#source("C:/Documents and Settings/pradip.muhuri/My
Documents/disorders_chart1.R") - Please ignore this line
#R Scripts for bar chart begin here
# Read drug data from tab-delimited data set
drug_data <- read.table("C:/Documents and Settings/pradip.muhuri/My
Documents/xdrug.dat", header=FALSE,
col.names=c("Age_1217", "Age_1825",
"Age_26Plus"),
row.names = c("White","Black","Native American/Alaska
Native","Hawaiian/OPI","Asian", "More than One
Race", "Hispanic"),
sep="\t")
# Graph drug use disorder data with adjacent bars using rainbow colors
barplot(as.matrix(drug_data), main="Past-Year Illicit Drug Use Disorders by
Race/Ethnicity", ylab= "Past-Year Use Disorder Rate (%)",
beside=TRUE,
col=rainbow(7))
legend("topright", c("White","Black","Native
American/Alaska
Native","Hawaiian/OPI","Asian", "More than One
Race", "Hispanic"), cex=0.6,
bty="n", fill=rainbow(7));
______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.