[R] Plotting factors in graph panel

Anupam Tyagi @nupty@g| @end|ng |rom gm@||@com
Mon Jul 3 11:53:31 CEST 2023


Attached is another example plot, that is better than the earlier one.

On Mon, 3 Jul 2023 at 15:21, Anupam Tyagi <anuptyagi using gmail.com> wrote:

> I thought maybe I can share with you how the data looks in Excel, and an
> example plot I found on the web that looks similar to what I want to plot.
> These are attached to this email as *.png files. I am trying to see (plot)
> how each row of data (percentages) varies with income, making many small
> graphs in the same plot. For each row of data there will be one graph. I
> can manually delete the "No Answer" rows in Excel, if that is the best
> solution. I want the output to look like the sparklines in column "I" of
> attached Excel screenshot, with labelling of each graphs. This is similar
> to the attached "Example_plot". I thought this could be done with Lattice,
> or base-R, or ggplot easily, but this turning out to be more difficult than
> I had thought.
>
> On Mon, 3 Jul 2023 at 14:38, Anupam Tyagi <anuptyagi using gmail.com> wrote:
>
>> Thanks Jim, thanks everyone. I was caught up with work and moving home,
>> so a delay in response. I tried running the code you provided and it is not
>> running well in my R-Studio setup. It is giving errors and not producing
>> plots. I don't yet understand all the code well yet, so I need to work on
>> it and then get back to you all. Sorry for not posting data from a R
>> dataframe. My data is still in Excel. I organized data in Excel almost
>> exactly (look wise) as the output from Stata log file (text) for a
>> "tabulate" command for a survey dataset. I don't yet understand a good way
>> to organize this data in R, so I cannot send it to you now. Let me do some
>> work on this, understand the R code you have given, and get back to you in
>> a few days. I have not been using R lately, but I think the graph I am
>> trying to make will be done better and easier in R than in Stata. Thank you
>> all for all your help. Let me do some work and get back to you.
>>
>>
>> On Fri, 30 Jun 2023 at 04:41, Jim Lemon <drjimlemon using gmail.com> wrote:
>>
>>> Okay. Here is a modification that does four single line plots.
>>>
>>> at_df<-read.table(text=
>>>  "Income MF MF_None MF_Equity MF_Debt MF_Hybrid Bank_None Bank_Current
>>> Bank_Savings Bank_NA
>>>  $10 1 3.05 29.76 31.18 36.0 46.54 24.75 25.4 3.307
>>>  $25 2 2.29 28.79 32.64 36.27 54.01 24.4 18.7 2.891
>>>  $40 3 2.24 29.51 34.31 33.94 59.1 25.0 29 13.4
>>>  $75 4 1.71 28.90 35.65 33.74 62.17 24.61 11.48 1.746
>>>  >$75 5 1.30 31.67 37.59 29.44 67.67 24.02 7.103 1.208
>>>  No_Answer 9 2.83 36.77 33.15 27.25 60.87 21.09 13.46 4.577",
>>>  header=TRUE,stringsAsFactors=FALSE)
>>> at_df<-at_df[at_df$Income!="No_Answer",which(names(at_df)!="Bank_NA")]
>>> png("Income_pcts.png",height=700)
>>> par(mfrow=c(4,1))
>>> plot(at_df[,"Bank_Current"],
>>>  type="l",lwd=3,main="Bsnk_Current",
>>>  xlab="Income",ylab="%",xaxt="n")
>>> axis(1,at=1:5,labels=at_df$Income)
>>> plot(at_df[,"Bank_Savings"],
>>>  type="l",lwd=3,main="Bank_Sasvings",
>>>  xlab="Income",ylab="%",xaxt="n")
>>> axis(1,at=1:5,labels=at_df$Income)
>>> plot(at_df[,"MF_Equity"],
>>>  type="l",lwd=3,main="MF_Equity",
>>>  xlab="Income",ylab="%",xaxt="n")
>>> axis(1,at=1:5,labels=at_df$Income)
>>> plot(at_df[,"MF_Debt"],
>>>  type="l",lwd=3,main="MF_Debt",
>>>  xlab="Income",ylab="%",xaxt="n")
>>> axis(1,at=1:5,labels=at_df$Income)
>>> dev.off()
>>>
>>> Jim
>>>
>>> On Thu, Jun 29, 2023 at 1:49 PM Anupam Tyagi <anuptyagi using gmail.com>
>>> wrote:
>>> >
>>> > Thanks, Pikal and Jim. Yes, it has been a long time Jim. I hope you
>>> have
>>> > been well.
>>> >
>>> > Pikal, thanks. Your solution may be close to what I want. I did not
>>> know
>>> > that I was posting in HTML. I just copied the data from Excel and
>>> posted in
>>> > the email in Gmail. The data is still in Excel, because I have not yet
>>> > figured out what is a good way to organize it in R. I am posting it
>>> again
>>> > below as text. These are rows in Excel: 1,2,3,5,9 after MF are income
>>> > categories and No Answer category (9). Down the second column are
>>> > categories of MF and Bank AC. Rest of the columns are percentages.
>>> >
>>> > Jim, thanks for the graph. I am looking to plot only one line
>>> (category)
>>> > each in many small plots on the same page. I don't want to compare
>>> > different categories on the same graph as you do, but see how each
>>> category
>>> > varies by income, one category in each graph. Like Excel does with
>>> > Sparklines (Top menu: Insert, Sparklines, Lines). I have many
>>> categories
>>> > for many variables. I am only showing two MF and Bank AC.
>>> >
>>> > Income $10 $25 $40 $75 > $75 No Answer
>>> > MF 1 2 3 4 5 9
>>> > None 1 3.05 2.29 2.24 1.71 1.30 2.83
>>> > Equity 2 29.76 28.79 29.51 28.90 31.67 36.77
>>> > Debt 3 31.18 32.64 34.31 35.65 37.59 33.15
>>> > Hybrid 4 36.00 36.27 33.94 33.74 29.44 27.25
>>> > Bank AC
>>> > None 1 46.54 54.01 59.1 62.17 67.67 60.87
>>> > Current 2 24.75 24.4 25 24.61 24.02 21.09
>>> > Savings 3 25.4 18.7 29 11.48 7.103 13.46
>>> > No Answer 9 3.307 2.891 13.4 1.746 1.208 4.577
>>> >
>>> >
>>> > On Wed, 28 Jun 2023 at 17:30, Jim Lemon <drjimlemon using gmail.com> wrote:
>>> >
>>> > > Hi Anupam,
>>> > > Haven't heard from you in a long time. Perhaps you want something
>>> like
>>> > > this:
>>> > >
>>> > > at_df<-read.table(text=
>>> > >  "Income MF MF_None MF_Equity MF_Debt MF_Hybrid Bank_None
>>> Bank_Current
>>> > > Bank_Savings Bank_NA
>>> > >  $10 1 3.05 29.76 31.18 36.0 46.54 24.75 25.4 3.307
>>> > >  $25 2 2.29 28.79 32.64 36.27 54.01 24.4 18.7 2.891
>>> > >  $40 3 2.24 29.51 34.31 33.94 59.1 25.0 29 13.4
>>> > >  $75 4 1.71 28.90 35.65 33.74 62.17 24.61 11.48 1.746
>>> > >  >$75 5 1.30 31.67 37.59 29.44 67.67 24.02 7.103 1.208
>>> > >  No_Answer 9 2.83 36.77 33.15 27.25 60.87 21.09 13.46 4.577",
>>> > >  header=TRUE,stringsAsFactors=FALSE)
>>> > >
>>> at_df<-at_df[at_df$Income!="No_Answer",which(names(at_df)!="Bank_NA")]
>>> > > png("MF_Bank.png",height=600)
>>> > > par(mfrow=c(2,1))
>>> > > matplot(at_df[,c("MF_None","MF_Equity","MF_Debt","MF_Hybrid")],
>>> > >  type="l",col=1:4,lty=1:4,lwd=3,
>>> > >  main="Percentages by Income and MF type",
>>> > >  xlab="Income",ylab="Percentage of group",xaxt="n")
>>> > > axis(1,at=1:5,labels=at_df$Income)
>>> > > legend(3,24,c("MF_None","MF_Equity","MF_Debt","MF_Hybrid"),
>>> > >  lty=1:4,lwd=3,col=1:4)
>>> > > matplot(at_df[,c("Bank_None","Bank_Current","Bank_Savings")],
>>> > >  type="l",col=1:3,lty=1:4,lwd=3,
>>> > >  main="Percentages by Income and Bank type",
>>> > >  xlab="Income",ylab="Percentage of group",xaxt="n")
>>> > > axis(1,at=1:5,labels=at_df$Income)
>>> > > legend(3,54,c("Bank_None","Bank_Current","Bank_Savings"),
>>> > >  lty=1:4,lwd=3,col=1:3)
>>> > > dev.off()
>>> > >
>>> > > Jim
>>> > >
>>> > > On Wed, Jun 28, 2023 at 6:33 PM Anupam Tyagi <anuptyagi using gmail.com>
>>> wrote:
>>> > > >
>>> > > > Hello,
>>> > > >
>>> > > > I want to plot the following kind of data (percentage of
>>> respondents
>>> > > from a
>>> > > > survey) that varies by Income into many small *line* graphs in a
>>> panel of
>>> > > > graphs. I want to omit "No Answer" categories. I want to see how
>>> each one
>>> > > > of the categories (percentages), "None", " Equity", etc. varies by
>>> > > Income.
>>> > > > How can I do this? How to organize the data well and how to plot? I
>>> > > thought
>>> > > > Lattice may be a good package to plot this, but I don't know for
>>> sure. I
>>> > > > prefer to do this in Base-R if possible, but I am open to ggplot.
>>> Any
>>> > > ideas
>>> > > > will be helpful.
>>> > > >
>>> > > > Income
>>> > > > $10 $25 $40 $75 > $75 No Answer
>>> > > > MF 1 2 3 4 5 9
>>> > > > None 1 3.05 2.29 2.24 1.71 1.30 2.83
>>> > > > Equity 2 29.76 28.79 29.51 28.90 31.67 36.77
>>> > > > Debt 3 31.18 32.64 34.31 35.65 37.59 33.15
>>> > > > Hybrid 4 36.00 36.27 33.94 33.74 29.44 27.25
>>> > > > Bank AC
>>> > > > None 1 46.54 54.01 59.1 62.17 67.67 60.87
>>> > > > Current 2 24.75 24.4 25 24.61 24.02 21.09
>>> > > > Savings 3 25.4 18.7 29 11.48 7.103 13.46
>>> > > > No Answer 9 3.307 2.891 13.4 1.746 1.208 4.577
>>> > > >
>>> > > > Thanks.
>>> > > > --
>>> > > > Anupam.
>>> > > >
>>> > > >         [[alternative HTML version deleted]]
>>> > > >
>>> > > > ______________________________________________
>>> > > > R-help using 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.
>>> > >
>>> >
>>> >
>>> > --
>>> > Anupam.
>>> >
>>> >         [[alternative HTML version deleted]]
>>> >
>>> > ______________________________________________
>>> > R-help using 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.
>>>
>>
>>
>> --
>> Anupam.
>>
>>
>
> --
> Anupam.
>
>

-- 
Anupam.

-------------- next part --------------
A non-text attachment was scrubbed...
Name: Example_plot2.png
Type: image/png
Size: 90776 bytes
Desc: not available
URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20230703/6c120593/attachment.png>


More information about the R-help mailing list