Gerard Smits
Gerard Smits

Reputation: 11

barplot is not showing colors correctly

r version: R 3.3.1 GUI 1.68 Mavericks build (7238)

I am using barplot to generate a waterfall plot for 103 subject. The data are in order of lowest to highest. Each row of my data frame has a color assigned in a variable called color (section of data shown below code).

I know my far right bar should be red, but it is showing up as blue. other error exist in the color assignment. Any suggestions appreciated.

Thanks,

Gerard

my code and data except follow:

pdf(file="//users//Gerard//GS//r_work//BCVA_PP_WF2.pdf")

par(oma=c(2,2,2,2));

barplot(d_bcva,
    space=0.0,
    ylim=c(-40,30),
        main = "Change in BCVA Letters Read at Week 20",
    col=color,
        ylab="Change",
    las=1,
        border=NA,
        cex.axis=1.0,
    cex.lab=1.0)
abline(h=c(-20,-15,-10,-5,5,10,15,20), col="lightgray", lty=c(2,2,2,2)) 

legend(
  "topleft",
  box.lty=0,
  legend=c("Group A", "Group B", "Group C", "Group D"),
  cex=0.8,
  fill=c('red','blue','green','black'))

dev.off()


        ARM d_bcva armn color order
1   Group C  -33.5    3 green     1
2   Group C  -26.0    3 green     2
3   Group B  -14.5    2  blue     3
4   Group D  -14.5    4 black     4
5   Group C  -11.0    3 green     5
6   Group B  -10.0    2  blue     6
7   Group A   -9.5    1   red     7
8   Group C   -7.5    3 green     8
9   Group C   -7.0    3 green     9
10  Group C   -7.0    3 green    10
11  Group C   -6.5    3 green    11

          [data removed]

97  Group D   15.0    4 black    97
98  Group D   16.0    4 black    98
99  Group A   16.5    1   red    99
100 Group B   16.5    2  blue   100
101 Group D   16.5    4 black   101
102 Group C   17.0    3 green   102
103 Group A   20.5    1   red   103

Upvotes: 1

Views: 778

Answers (1)

desc
desc

Reputation: 1210

The color column was coming back as a factor and affecting your call for colors from each row.

Assuming your data is in a data.frame named x:

barplot(x$d_bcva,space=0.0,ylim=c(-40,30),
    main = "Change in BCVA Letters Read at Week 20",
    col=as.vector(x$color), ylab="Change", las=1,border=NA,
    cex.axis=1.0, cex.lab=1.0)

abline(h=c(-20,-15,-10,-5,5,10,15,20), col="lightgray", lty=c(2,2,2,2)) 

Upvotes: 1

Related Questions