[R] A question on graphical representation

Jim Lemon jim at bitwrit.com.au
Sun Mar 2 01:47:14 CET 2014


On 03/02/2014 05:51 AM, Ron Michael wrote:
> Hi,
>
> Let say I Have following data:
>
>> Info<- structure(list(Person = structure(1:6, .Label = c("A", "B", "C",
> + "D", "E", "F"), class = "factor"), Attr1 = c(0.52, 0.14, 0.63,
> + 0.43, 0.89, 18.46), Attr2 = c(0.06, 3.35, 0.62, 1.42, 1.96, 8.38
> + )), .Names = c("Person", "Attr1", "Attr2"), row.names = c(NA,
> + -6L), class = "data.frame")
>> Info
>    Person Attr1 Attr2
> 1      A  0.52  0.06
> 2      B  0.14  3.35
> 3      C  0.63  0.62
> 4      D  0.43  1.42
> 5      E  0.89  1.96
> 6      F 18.46  8.38
>> Diff<- Info[, 'Attr1'] - Info[, 'Attr2']
>> Diff
> [1]  0.46 -3.21  0.01 -0.99 -1.07 10.08
>> Overall_Diff<- sum(Diff)
>> Overall_Diff
> [1] 5.28
>
>
>
> Now we see that the overall difference between attribute-1 and attribute-2 for persons A-F is 5.28. Now I was asked this question: tell me out of that overall difference, how much is attributed to person A, person B,.... person F? In case the 'Diff' variable all have positive values, I probably consider making a pie-chart. However this is not possible (probably) in this present case, because some values are negative and some values are positive.
>
> So my question is how to build a robust graphical representation to answer this question?
>
Hi Ron,
I'm guessing as to exactly what you want, but try this:

barpos<-barplot(c(Diff,sum(Diff)),horiz=TRUE)
mtext(c(LETTERS[1:6],"Sum"),side=2,at=barpos,line=1)

If you want to display the values as well, add:

require(plotrix)
barlabels(c(Diff,sum(Diff))/2,barpos,c(Diff,sum(Diff)),prop=1)

Jim




More information about the R-help mailing list