[R] about plotting a special case

Jim Lemon drjimlemon at gmail.com
Thu Jul 13 10:35:25 CEST 2017


If you want colors mapped to the _values_ in DF1$C, there are a number
of ways to do it:

Color_unq<-color.scale(DF1$C,c(1,0),c(0,0,c(0,1))

This will produce colors from the lowest values (red) through the
highest (blue). See the help page for color.scale to get different
colors. With this you can use color.legend to add a mapping of the
values and colors.

If you just want different colors, say 20, you can do things like:

Color_unq<-rainbow(20)
Color_unq<-colors(distinct=TRUE)[1:20]

Jim

On Thu, Jul 13, 2017 at 4:22 PM, lily li <chocold12 at gmail.com> wrote:
> Hi Jim,
>
> How to generate unique (all different) colors for the same dataframe? I use
> the code below, but found that there are same colors. Thanks again.
>
> Color= as.factor(DF1$C)
> Color.unq = unique(Color)
> points(DF1$A,DF1$B, col=Color.unq, pch=19)
>
>
> On Wed, Jul 12, 2017 at 10:02 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
>>
>> values A and B have different ranges. Therefore you would want
>> horizontal error bars for the B mean. The dispersion function doesn't
>> do horizontal error bars, so you can use plotCI also in plotrix. Here
>> is an example with two databases:
>>
>> DF2<-read.table(text="A  B  C
>>      62 22 54
>>      69 24 55
>>      51 28 56
>>      47 25 53
>>      70 21 52
>>      61 23 50
>>      40 19 51
>>      58 18 57
>>      38 24 58
>>      37 27 53",header=TRUE)
>> plot(DF1$A,DF1$B,xlim=range(c(DF1$A,DF2$A)),
>>  ylim=range(c(DF1$B,DF2$B)),pch=19,col="red")
>> meanA<-mean(DF1$A)
>> meanB<-mean(DF1$B)
>> points(meanA,meanB,pch=18,col="red")
>> q1090<-quantile(DF1$B,probs=c(0.1,0.9))
>> plotCI(meanA,meanB,ui=q1090[2],li=q1090[1],err="x",
>>   add=TRUE,col="red")
>> points(DF2$A,DF2$B,pch=19,col="green")
>> meanA<-mean(DF2$A)
>> meanB<-mean(DF2$B)
>> points(meanA,meanB,pch=18,col="green")
>> q1090<-quantile(DF2$B,probs=c(0.1,0.9))
>> plotCI(meanA,meanB,ui=q1090[2],li=q1090[1],err="x",
>>  add=TRUE,col="red")
>> # click at the upper left corner of the legend
>> legend(locator(1),legend=c("DF1","DF2"),pch=19,
>>  col=c("red","green"))
>>
>> Jim
>>
>> On Thu, Jul 13, 2017 at 1:32 PM, lily li <chocold12 at gmail.com> wrote:
>> > Thanks, Jim. The code works, but I don't understand why you use q1090 <-
>> > quantile(DF1$B, probs=c()), rather than DF1$A? Also, how to add a legend
>> > for
>> > both points DF1 and DF2?
>> >
>> > On Wed, Jul 12, 2017 at 8:25 PM, Jim Lemon <drjimlemon at gmail.com> wrote:
>> >>
>> >> Hi lily,
>> >> Here is the first plot:
>> >>
>> >> plot(DF1$A,DF1$B,pch=19,col="red")
>> >> meanA<-mean(DF1$A)
>> >> meanB<-mean(DF1$B)
>> >> points(meanA,meanB,pch=18,col="red")
>> >> q1090<-quantile(DF1$B,probs=c(0.1,0.9))
>> >> library(plotrix)
>> >> dispersion(meanA,meanB,q1090[2],q1090[1],
>> >>  intervals=FALSE,col="red")
>> >>
>> >> The same code will work for a second data frame, except that you would
>> >> use "points" instead of "plot" and change the color. You may also have
>> >> to specify xlim and ylim in the first call to "plot" so that all
>> >> values are on the plot.
>> >>
>> >> Jim
>> >>
>> >>
>> >>
>> >> On Thu, Jul 13, 2017 at 11:46 AM, lily li <chocold12 at gmail.com> wrote:
>> >> > Hi R users,
>> >> >
>> >> > I have a question about plotting. There is the dataframe below, while
>> >> > each
>> >> > row represents a record. If I want to plot on a A-B plot, i.e.,
>> >> > x-axis
>> >> > represents A, while y-axis represents B values. However, I want to
>> >> > plot
>> >> > the
>> >> > mean value from records 1-10 as one point, while the 10th and 90th
>> >> > percentiles represent the error bars, such as one point in the
>> >> > attached
>> >> > example. I don't know how to do this, and then add a legend.
>> >> > After the above step, if I have a dataframe DF2 with the same
>> >> > structure
>> >> > but
>> >> > different values than DF1, how to show the point on the same figure,
>> >> > but
>> >> > use different colors or symbols? Thanks for any advices.
>> >> >
>> >> > DF1
>> >> >
>> >> >         A  B  C
>> >> > 1     65 21 54
>> >> > 2     66 23 55
>> >> > 3     54 24 56
>> >> > 4     44 23 53
>> >> > 5     67 22 52
>> >> > 6     66 21 50
>> >> > 7     45 20 51
>> >> > 8     56 19 57
>> >> > 9     40 25 58
>> >> > 10   39 24 53
>> >> >
>> >> > ______________________________________________
>> >> > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> >> > 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.
>> >
>> >
>
>



More information about the R-help mailing list