I am new to the world of R/programming so this may be a really easy question. I thank you for your patience and help in advance I would like the characters km^2 to be displayed on the plot subtitle as km squared - two as a superscript. I would also like to have the numbers from the data set for longitude and latitude to be rounded to four decimal places. Thank you. plot ( decade[['date']], decade[['value']], type = 'l', col = 'lightsteelblue4', ylab = 'Discharge [cms]', main = sprintf('%s [%s]', stn[['metadata']][['name']], stn[['metadata']][['id']]), km^2 <- expression sub = sprintf('Seasonal station with natural streamflow - Lat: %s Lon: %s Gross Area %s km^2 - Effective Area %s km^2', stn[['metadata']][['latitude']], stn[['metadata']][['longitude']],stn[['metadata']][['grossarea']], stn[['metadata']][['effectivearea']]), cex.sub = 1, font.sub = 3, col.sub = "black" ) -- View this message in context: http://www.nabble.com/Superscripts-and-rounding-tp24682319p24682319.html Sent from the R help mailing list archive at Nabble.com.
ehux wrote:> I am new to the world of R/programming so this may be a really easy question. > I thank you for your patience and help in advance > > I would like the characters km^2 to be displayed on the plot subtitle as km > squared - two as a superscript. > > I would also like to have the numbers from the data set for longitude and > latitude to be rounded to four decimal places. > > Thank you. > > plot ( > decade[['date']], > decade[['value']], > > type = 'l', > col = 'lightsteelblue4', > ylab = 'Discharge [cms]', > main = sprintf('%s [%s]', stn[['metadata']][['name']], > stn[['metadata']][['id']]), > km^2 <- expression > sub = sprintf('Seasonal station with natural streamflow - Lat: %s Lon: %s > Gross Area %s km^2 - Effective Area %s km^2', > stn[['metadata']][['latitude']], > stn[['metadata']][['longitude']],stn[['metadata']][['grossarea']], > stn[['metadata']][['effectivearea']]), > cex.sub = 1, font.sub = 3, col.sub = "black" > )Since I do not have the data I can only guess: sub = substitute('Seasonal station with natural streamflow - Lat:' * a * ' Lon:' * b * ' Gross Area ' * c * km^2 * ' - Effective Area ' * d * km^2', list(a = stn[['metadata']][['latitude']], b = stn[['metadata']][['longitude']], c = stn[['metadata']][['grossarea']], d = stn[['metadata']][['effectivearea']])) Uwe Ligges
sub = sprintf('Seasonal station with natural streamflow - Lat: %s Lon: %s Gross Area %s km^2 - Effective Area %s km^2', stn[['metadata']][['latitude']], stn[['metadata']][['longitude']],stn[['metadata']][['grossarea']], stn[['metadata']][['effectivearea']]), I tried your code and a few variations, but was able to make it work - Uwe (but thank you!!) I am having no problem with retrieving the data for the above code. i would just like the sub-title to display the units after the data correctly (aka km suberscript 2 - instead of km^2). In addition i want the numbers being pulled from the database to round to four decimal places as currently the lat and lon come in as huge numbers. I have tried a to use the round command but it always just shows up as text in my title???? -- View this message in context: http://www.nabble.com/Superscripts-and-rounding-tp24682319p24701709.html Sent from the R help mailing list archive at Nabble.com.
Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Jul-28 18:03 UTC
[R] Superscripts and rounding
I'm anything but an expert in R however if I'm labeling a graph axis with a superscript I have tended to use:> plot (x , y , xlab = expression ("label"^2))But when you try to have more than one superscript it fails. Assuming you are in a UTF8 location (Western Europe) you could try:> plot (x , y , xlab = expression ("Some label text \UB2 some more label text \UB2"))That works for me. (B2 is the hex code for UTF-8 character = ^2 and \U is a control sequence that will call that character.) It onlyu works if you are a UTF8 area from what i understand. -- As for rounding - can you not populate a new field using the round command? i.e. something like [metadata][long_4dig] = round([metadata][longitude],4) #I haven't used round before my syntax may be wrong! Then use %s to drop that value in? C ******************************************************************************************************************** This message may contain confidential information. If yo...{{dropped:21}}
Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Jul-28 21:26 UTC
[R] Superscripts and rounding
Slightly confused because if I try:> newdata.yaxis = c(2.0000473, 3.123456, 3.23456, 2.67890, 1.56789) > newdata.yaxis_4 = round (newdata.yaxis, digits = 4) > newdata.yaxis[1] 2.000047 3.123456 3.234560 2.678900 1.567890> newdata.yaxis_4[1] 2.0000 3.1235 3.2346 2.6789 1.5679 As you see - I get a new variable with the result rounded to 4 places. Then if I try:> inserted = sprintf("Some text with a 4 digit number ( %s ) inserted in it", newdata.yaxis_4[2]) > inserted[1] "Some text with a 4 digit number ( 3.1235 ) inserted in it" It works for me - clearly the way my data is structured is a little different from you. Would have thought you might need to do something like:> [['rounddata']] = round ([['metadata']], digits = 4)Then call your data with [['rounddata']][['latitude']] etc - I'm no expert on all this matrixes stuff though! (Not even sure what a double [[ means! ) But this (calling the round within the sprintf function) also works for me:> inserted = sprintf("Some text with a 4 digit number ( %s ) inserted in it",round( newdata.yaxis[2], digits = 4)) > inserted[1] "Some text with a 4 digit number ( 3.1235 ) inserted in it" So why can't you just use:> sub = sprintf('Seasonal station with natural streamflow - Lat: %s Lon: %s Gross Area %s km\UB2 - Effective Area %s km\UB2,+ round( [['metadata']][['latitude']], digits = 4), + round( [['metadata']][['longitude']], digits = 4), + round( [['metadata']][['grossarea']], digits = 4), + round( [['metadata']][['effectivearea']] digits = 4), + ) ******************************************************************************************************************** This message may contain confidential information. If yo...{{dropped:21}}
Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Jul-29 23:48 UTC
[R] Superscripts and rounding
> > library(RODBC) > library(HYDAT) > You will need to install HYDAT (the zip file) from > http://www.geog.ubc.ca/~rdmoore/Rcode.htm > > Below is my current code - which works. The [[]] is the way i am accessing > the columns from the data frame. > > thanks again for all your help!!!! > > # load HYDAT data > par(mfrow=c(3,1)) > path <- 'c:\\HYDAT' > wsc.id <- '11AB075' > stn <- ReadHydatFile(path, wsc.id) #if data in CDROM image > # stn <- ReadHydatDatabase (path, wsc.id) #if data in database >I'd need access to the whole data file. I tried exporting some data from the website for it but it got too complex for me! However, it seems to me you have two chunks of data: stn[flow] - which has daily flow data in it? stn[metadata] which i guess is a header for the whole dataset -describing what it is. So I recreated what i hope might be simillar to part of your data (using lst instead of stn as the vector/array/list name) # Build an array containing a lat & long. info <- data.frame(latitude=1.0005689, longitude=55.698754) #display result info # latitude longitude #1 1.000569 55.69875 #create a list (called lst) with an object metadata in it containing the array lst <- list (metadata=info) #display result lst #$metadata # latitude longitude #1 1.000569 55.69875 #check if can call a single piece of data using the square brackets as references... lst[['metadata']][['longitude']] #[1] 55.69875 #now try rounding that> round (lst[['metadata']][['longitude']], digits=2)[1] 55.7 #now try sprintf'ing that sprintf('Seasonal station with natural streamflow - Lat: %s', round (lst[['metadata']][['longitude']], digits=2)) # [1] "Seasonal station with natural streamflow - Lat: 55.7" #now try that in a plot plot(1,1, sub=sprintf('Seasonal station with natural streamflow - Lat: %s', round (lst[['metadata']][['longitude']], digits=2))) # results in a correct label ;-) Its possible to refer to that same value in the following ways:> lst$metadata # same as lst[['metadata']]latitude longitude 1 1.000569 55.69875> lst$metadata[['longitude']] # same as lst[['metadata']][['longitude']][1] 55.69875> lst$metadata$longitude # same as lst[['metadata']][['longitude']][1] 55.69875>So I'm stumped! without being able to see the actual structure of your data I can't figure out why you are getting an error! BTW - was there a cut and paste error? Your error message was reported as: Error: unexpected symbol in: " sub = sprintf('Seasonal station with natural streamflow - Lat: %s Lon: %s Gross Area %s km\UB2 - Effective Area %s km\UB2, + round( [['metadata"> + round( [['metadata']][['longitude']], digits = 4),Error: unexpected '[[' in "+ round( [[" The + on + round looks like the plus was typed. + shouldn't have been typed, but also was there a missing quote after the \UB2. You should have entered: sub = sprintf('Seasonal station with natural streamflow - Lat: %s Lon: %s Gross Area %s km\UB2 - Effective Area %s km\UB2 ', round( [['metadata']][['latitude']], digits=4 ), round( [['metadata']][['longitude']], digits = 4), round( [['metadata']][['grossarea']], digits = 4), round( [['metadata']][['effectivearea']], digits = 4)) C ******************************************************************************************************************** This message may contain confidential information. If yo...{{dropped:21}}
Polwart Calum (County Durham and Darlington NHS Foundation Trust)
2009-Jul-30 20:27 UTC
[R] Superscripts and rounding
Duh! did it again! the variables need str in front of them don't they!! sub = sprintf('Seasonal station with natural streamflow - Lat: %s Lon: %s Gross Area %s km\UB2 - Effective Area %s km\UB2 ', round( str[['metadata']][['latitude']], digits=4 ), round( str[['metadata']][['longitude']], digits = 4), round( str[['metadata']][['grossarea']], digits = 4), round( str[['metadata']][['effectivearea']], digits = 4)) Let me know if that works. ******************************************************************************************************************** This message may contain confidential information. If yo...{{dropped:21}}