首页 > 解决方案 > coor_trans to adapt the data dynamically in ggplot2

问题描述

I am currently doing a faceted visualization as below.

enter image description here

So far so good...

To bring in further sense I wish I could transform the X axis and Y axis as per the percentage contribution ...

What I mean e.g. for 17549(alpha) facet :

1.The X axis between -45 to -85 should cover 3% of X axis space , -85 to -95 to cover 11% and so on dynamically.

2.Similarly, for Y axis between -3 to -10 should cover 39% (3+9+16+10+1) of Y axis range ,-10 to -14 should cover 42%(2+11+16+9+4) ...and so on

After lots of research it appears to me the way lies in creating an equation that can fit the variation above and then creating a trans_new. I have a notion that finding a linear equation to fit the axis will not be possible as there is discrete variation ...

I hope I have been able to frame the problem statement clearly. Would appreciate some help.

...

**Ok on a further detailing i have proceeded to make the current visualization as below****

1.first i create the smooth lines with the following data

my data is as below

> dput(head(rqamc,4))
structure(list(sidentity = c("17549(alpha)", "17549(alpha)", 
"17549(alpha)", "17549(alpha)"), band.x = c("2300-c1", "2300-c1", 
"2300-c1", "2300-c1"), band.y = c("2300-c1", "2300-c1", "2300-c1", 
"2300-c1"), measuredSrvRsrp = c(-108, -112, -103, -118), modersrq = c(-8.75, 
-12.25, -7.75, -14.25), avgrsrq = c(-9.75, -11.25, -7.75, -13.25
), quantrsrq = c(25.65, 24, 27, 19.25), rsrqdistr = list(c(26L, 
19L, 22L, 19L, 20L, 18L, 19L, 22L, 22L, 22L, 24L, 25L, 19L, 21L, 
19L, 22L, 18L, 24L, 21L, 13L, 10L, 20L, 22L, 16L, 26L, 26L, 22L, 
22L, 21L, 14L, 21L, 22L, 12L, 7L, 21L, 21L, 21L, 21L, 25L, 25L, 
24L, 23L, 25L, 18L, 22L, 24L, 22L, 19L), c(15L, 20L, 16L, 15L, 
12L, 15L, 18L, 19L, 25L, 18L, 19L, 22L, 15L, 15L, 16L, 18L, 15L, 
20L, 20L, 25L, 15L, 22L, 22L, 10L, 10L, 22L, 20L, 20L, 21L, 18L, 
16L, 23L, 22L, 24L, 14L, 2L, 9L, 11L, 17L, 22L, 19L), c(18L, 
24L, 26L, 27L, 23L, 23L, 22L, 22L, 27L, 27L, 25L, 25L, 24L, 23L, 
25L, 23L, 22L, 25L, 24L, 19L, 27L, 27L, 26L, 26L, 27L, 23L, 25L, 
24L, 23L, 24L, 24L, 23L, 24L, 28L, 24L, 24L, 24L, 24L, 24L, 24L, 
24L, 23L, 23L, 25L, 28L, 24L, 23L, 25L, 22L, 18L), c(21L, 13L, 
15L, 11L, 12L, 16L, 10L, 11L)), meanvsmode = c(-1.4375, 2.48780487804878, 
0.0799999999999983, 2.625)), .Names = c("sidentity", "band.x", 
"band.y", "measuredSrvRsrp", "modersrq", "avgrsrq", "quantrsrq", 
"rsrqdistr", "meanvsmode"), class = c("data.table", "data.frame"
), row.names = c(NA, -4L), .internal.selfref = <pointer: 0x00000000027d0788>)

on this data i do the following

g<-ggplot(data=rqamc)+geom_smooth(data=rqamc,aes(x=measuredSrvRsrp,y=as.integer(modersrq),colour=as.factor(band.x)),method = "auto",se=FALSE)+theme(legend.position = "top",legend.direction = "horizontal",axis.text.x = element_text(angle =60, hjust = 1))+scale_x_reverse(breaks=seq(-45, -140, -3),expand=c(0,0))+scale_y_reverse(breaks=seq(-3, -24, -1))+facet_wrap(sidentity~.,scale="free",ncol = 1)+ggtitle(paste0("RSRP-RSRQ Genaralized additive -Regression Model for NGBR Multicarrier Trigger Points For Circle ",mccmnc,"for ",endb[i]))

this gives me step1

post this i draw the colour tiles and rectangles using the following data

> dput(rsrprsrqtiles)
structure(list(xmin = c(-45L, -85L, -95L, -105L, -113L, -119L, 
-45L, -85L, -95L, -105L, -113L, -119L, -45L, -85L, -95L, -105L, 
-113L, -119L, -45L, -85L, -95L, -105L, -113L, -119L, -45L, -85L, 
-95L, -105L, -113L, -119L), xmax = c(-85L, -95L, -105L, -113L, 
-119L, -140L, -85L, -95L, -105L, -113L, -119L, -140L, -85L, -95L, 
-105L, -113L, -119L, -140L, -85L, -95L, -105L, -113L, -119L, 
-140L, -85L, -95L, -105L, -113L, -119L, -140L), ymin = c(-3L, 
-3L, -3L, -3L, -3L, -3L, -10L, -10L, -10L, -10L, -10L, -10L, 
-14L, -14L, -14L, -14L, -14L, -14L, -17L, -17L, -17L, -17L, -17L, 
-17L, -19L, -19L, -19L, -19L, -19L, -19L), ymax = c(-10L, -10L, 
-10L, -10L, -10L, -10L, -14L, -14L, -14L, -14L, -14L, -14L, -17L, 
-17L, -17L, -17L, -17L, -17L, -19L, -19L, -19L, -19L, -19L, -19L, 
-24L, -24L, -24L, -24L, -24L, -24L), tilecolor = c("green", "lightgreen", 
"yellow", "orange", "red", "red", "lightgreen", "lightgreen", 
"lightgreen", "lightgreen", "lightgreen", "lightgreen", "red", 
"red", "red", "orange", "orange", "orange", "red", "red", "red", 
"red", "red", "red", "red", "red", "red", "red", "red", "red"
), category = 1:30), .Names = c("xmin", "xmax", "ymin", "ymax", 
"tilecolor", "category"), row.names = c(NA, -30L), class = c("data.table", 
"data.frame"), .internal.selfref = <pointer: 0x00000000027d0788>)

and the following code

tilecreation<-geom_rect(data=rsrprsrqtiles,aes(xmin=xmin,xmax=xmax,ymin=ymin,ymax=ymax,fill=tilecolor),alpha=0.2,colour="blue")

g1<-g+tilecreation+scale_fill_manual(values=c("green"="green","red"="red","yellow"="yellow","lightgreen"="lightgreen","orange"="orange"))

to produce

step2

after this i annotatate each category rectangle as follows

the data for that

> dput(head(rqascales,4))
structure(list(sidentity = c("17549(alpha)", "17549(beta)", "17549(gamma)", 
"17549(alpha)"), category = c(1L, 1L, 1L, 2L), bandcomb = c("2300-c1/2300-c2/1800-c1/850-c1", 
"2300-c1/2300-c2/1800-c1/850-c1", "2300-c1/2300-c2/1800-c1/850-c1", 
"2300-c1/2300-c2/1800-c1/850-c1"), totalsample = c(52066L, 151251L, 
90452L, 52066L), persecpercategorysample = c(1404L, 372L, 1204L, 
4515L), persecperbandpercategorysample = list(c(260L, 213L, 78L, 
853L), c(97L, 46L, 41L, 188L), c(135L, 30L, 7L, 1032L), c(659L, 
696L, 251L, 2909L)), pccattothesector = c(3, 0, 1, 9), pcbandtothecategory = list(
    c(19, 15, 6, 61), c(26, 12, 11, 51), c(11, 2, 1, 86), c(15, 
    15, 6, 64)), tilecolor = c("green", "green", "green", "lightgreen"
), xpoint = c(-47, -47, -47, -87), ypoint = c(-6.5, -6.5, -6.5, 
-6.5), bandcontri = c("19,15,6,61", "26,12,11,51", "11,2,1,86", 
"15,15,6,64"), bandcontriab = c("2.6 h-2.1 h-78 -8.5 h", "97 -46 -41 -1.9 h", 
"1.4 h-30 -7 -1 K", "6.6 h-7 h-2.5 h-2.9 K")), .Names = c("sidentity", 
"category", "bandcomb", "totalsample", "persecpercategorysample", 
"persecperbandpercategorysample", "pccattothesector", "pcbandtothecategory", 
"tilecolor", "xpoint", "ypoint", "bandcontri", "bandcontriab"
), class = c("data.table", "data.frame"), row.names = c(NA, -4L
), .internal.selfref = <pointer: 0x00000000027d0788>)

and the code

g1<-g1+geom_text(data=rqascales,aes(x=xpoint,y=ypoint,label=paste0(formatter(pccattothesector),"%"),size=pccattothesector))+scale_size(range = c(4,8))

which produces my final output

the final one

what i want.. is to appropriation the demarcation across x axis and y axis in proportion to the percentage contribution...

e.g since the first demarcation on x axis is at -85.. for the first facet of 17549(alpha) i need to have this line at 3% ( 3+0+0) of the x axis span ..so essentially for this facet i want the range of -45 to -85 to be spanning 3% of the x axis length...the range -85 to -95 spanning 11%(9%+2%+0%).. the range -95 to -105 spanning 28%(16%+11%+1%+0%).... and so on.

same is for y axis as well..

i am trying to figure out how to do this coord_trans..or something else

hope i was able to elaborate rightly.. thanks for your attention

标签: rggplot2

解决方案


推荐阅读