[R] About doing figures

Jim Lemon drjimlemon at gmail.com
Mon Jul 17 00:33:50 CEST 2017


Hi lily,
To answer your questions more or less in order:

rainbow() is an easy way to get a small number of distinct colors. You
only had nine unique values in "dfm$A". Obviously it gets harder to
distinguish colors when you have more of them, so just increasing the
number that rainbow() returns will eventually make some of the colors
hard to distinguish. You can use any method of getting distinct colors
that you want, but simply indexing into colors() does not guarantee
you will get distinct colors.

As you had used the "ncol" argument in your example, I thought you
knew how to stretch out the legend horizontally. I only added the
second legend as a suggestion. How does the viewer know about the
symbols and DF?

Jim


On Mon, Jul 17, 2017 at 1:30 AM, lily li <chocold12 at gmail.com> wrote:
> For more than 10 records, how to reformat the colors? Also, how to show the
> first legend only, but at the bottom, while the second legend in your code
> is not necessary? In all, the same A values have the same color, but
> different symbols in DF==1 and DF==2.
> Thanks for your help.
>
> On Sun, Jul 16, 2017 at 9:28 AM, lily li <chocold12 at gmail.com> wrote:
>>
>> Hi Jim,
>>
>> For true color, I meant that the points in the figure do not correspond to
>> the values from the dataframe. Also, why to use rainbow(9) here? And the
>> legend is straight in the middle, is it possible to reformat it to the very
>> bottom? Thanks again.
>>
>> On Sun, Jul 16, 2017 at 2:50 AM, Jim Lemon <drjimlemon at gmail.com> wrote:
>>>
>>> Hi lily,
>>> As I have no idea of what the "true record" is, I can only guess.
>>> Maybe this will help:
>>>
>>> # get some fairly distinct colors
>>> rainbow_colors<-rainbow(9)
>>> # this should sort the numbers in dfm$A
>>> dfm$Acolor<-factor(dfm$A)
>>> plot(dfm$B,dfm$C,pch=ifelse(dfm$DF==1,1,19),
>>>  col=rainbow_colors[as.numeric(dfm$Acolor)])
>>> legend("bottom",legend=sort(unique(dfm$A)),
>>>  fill=rainbow_colors)
>>> legend(25,35,c("DF=1","DF=2"),pch=c(1,19))
>>>
>>> Jim
>>>
>>>
>>> On Sun, Jul 16, 2017 at 3:43 PM, lily li <chocold12 at gmail.com> wrote:
>>> > Hi R users,
>>> >
>>> > I still have the problem about plotting. I wanted to put the datasets
>>> > on
>>> > one figure, x-axis represents values B, y-axis represents values C,
>>> > while
>>> > different colors label column A. Each record uses a circle on the
>>> > figure,
>>> > while hollow circles represent DF=1 and solid circles represent DF=2. I
>>> > put
>>> > my code below, but the A labels do not correspond to the true record,
>>> > so I
>>> > don't know what is the problem. Thanks for your help.
>>> >
>>> > dfm
>>> > dfm1= subset(dfm, DF==1)
>>> > dfm2= subset(dfm, DF==2)
>>> > plot(c(15:30),seq(from=0,to=60,by=4),pch=19,col=NULL,xlab='Value
>>> > B',ylab='Value C')
>>> > Color = as.factor(dfm1$A)
>>> > colordist = grDevices::colors()[grep('gr(a|e)y', grDevices::colors(),
>>> > invert = T)] # for unique colors
>>> > Color.unq = sample(colordist,length(Color))
>>> >
>>> > points(dfm1[,3],dfm1[,4],col=Color.unq,pch=1)
>>> > points(dfm2[,3],dfm2[,4],col=Color.unq,pch=19)
>>> >
>>> > legend('bottom',as.character(Color.unq),col=Color.unq,lwd=rep(2,length(Color.unq)),cex=.6,ncol=5)
>>> >
>>> > legend('bottom',as.character(Color),col=Color.unq,lwd=3,cex=.6,ncol=5,text.width=c(9.55,9.6,9.55))
>>> >
>>> > dfm is the dataframe below.
>>> >
>>> > DF   A  B  C
>>> > 1     65 21 54
>>> > 1     66 23 55
>>> > 1     54 24 56
>>> > 1     44 23 53
>>> > 1     67 22 52
>>> > 1     66 21 50
>>> > 1     45 20 51
>>> > 1     56 19 57
>>> > 1     40 25 58
>>> > 1     39 24 53
>>> > 2     65 25 52
>>> > 2     66 20 50
>>> > 2     54 21 48
>>> > 2     44 30 49
>>> > 2     67 27 50
>>> > 2     66 20 30
>>> > 2     45 25 56
>>> > 2     56 14 51
>>> > 2     40 29 48
>>> > 2     39 29 23
>>> >
>>> >         [[alternative HTML version deleted]]
>>> >
>>> > ______________________________________________
>>> > 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