Heatmap in r with geom_raster - r

I am trying to plot a heat map from data with three variables. I am using ggplot with geom_raster, but doesn't seem to work. I am unable to see what's going wrong.
library(tidyverse)
p <- read.csv("Rheatmaptest.csv", header = TRUE);
p
xdir ydir Category.1 Category.2 Category.3 Category.4
1 -10.731 10.153 0.61975 3.2650 0.19025 13.00
2 -21.462 9.847 1.77000 3.2475 0.56325 16.70
3 -32.193 9.847 1.65500 2.9900 0.51325 176.00
4 -42.924 10.000 1.34500 3.1800 0.41350 177.00
5 -16.770 20.000 0.69600 3.4975 0.22150 174.00
6 -33.541 20.000 0.68700 3.4275 0.20250 4.24
7 -50.311 20.000 0.77350 3.1575 0.24250 177.00
8 -67.082 20.000 1.09600 3.5350 0.34600 163.00
9 -18.689 30.000 0.54250 3.5875 0.18100 160.00
10 -37.378 30.000 0.63075 3.7125 0.19300 158.00
11 -56.067 30.000 0.71975 3.5425 0.22225 2.26
12 -74.756 30.000 0.79100 3.3750 0.23000 8.24
13 -20.000 40.000 0.76650 3.7200 0.24375 167.00
14 -40.000 40.000 0.68325 3.5300 0.21350 155.00
15 -60.000 40.000 0.81075 3.3400 0.25325 145.00
16 -80.000 40.000 0.68800 3.6375 0.21350 146.00
17 -19.521 50.000 0.67900 3.7150 0.21700 167.00
18 -39.043 50.000 0.69500 3.7950 0.21225 109.00
19 -58.564 49.847 0.68300 3.5575 0.20700 166.00
20 -78.085 50.000 0.67375 3.5325 0.21975 163.00
21 -17.562 60.000 0.64350 3.7025 0.19475 140.00
22 -35.585 60.000 0.56650 3.5250 0.17775 34.30
23 -54.067 60.000 0.82350 3.7700 0.24525 129.00
24 -72.090 60.000 0.85450 3.6675 0.28225 156.00
25 -15.522 70.000 0.59100 3.3475 0.18875 144.00
26 -31.044 69.847 0.56200 3.7975 0.17250 159.00
27 -46.566 70.000 0.79375 3.5350 0.24975 145.00
28 -62.088 70.000 0.64275 3.6100 0.20375 132.00
29 -11.040 80.000 0.75875 3.7450 0.23925 138.00
30 -22.081 80.000 0.81900 3.3875 0.25975 144.00
31 -33.121 80.000 0.72725 3.5825 0.22175 132.00
32 -44.161 80.000 0.83300 3.5550 0.27000 177.00
33 -4.522 90.000 1.77500 3.1250 0.57200 16.30
34 -9.440 90.000 0.96925 3.7200 0.31000 163.00
35 -13.106 90.000 0.76975 3.6600 0.23800 3.50
36 -18.089 90.000 0.86050 3.6750 0.26650 80.50
ggplot(p, aes(x = xdir, y = ydir)) +
geom_raster(aes(fill = Category.1), interpolate = TRUE) +
scale_fill_gradient2(limits=c(0.5,2), low="blue", mid="yellow", high="red", midpoint=1)
I am able to see points when I use geom_point instead of geom_raster. Even with geom_raster, I just see very tiny points at the corresponding locations. Interpolate doesn't seem to work.
Am I missing something?

The implied precision of your data is causing your rasters to be plotted so small they are barely visible.
By reducing your precision, you can at least see your raster plot though it is still probably not very useful. Posting this I see I came to the same solution as #tifu.
db %>%
ggplot(aes(x = round(xdir/2), y = round(ydir), fill = Category.1)) +
geom_raster(aes(fill = Category.1)) +
scale_fill_gradient2(limits=c(0.5,2), low="blue", mid="yellow", high="red", midpoint=1)

Related

Barplot using ggplot2 for 4 variables

I have data like this:
ID height S1 S2 S3
1 927 0.90695438 0.28872194 0.67114294
2 777 0.20981677 0.71783084 0.74498220
3 1659 0.35813799 0.92339744 0.44001698
4 174 0.44829914 0.67493949 0.11503942
5 1408 0.90642643 0.18593999 0.67564278
6 1454 0.38943930 0.34806716 0.73155952
7 2438 0.51745975 0.12351953 0.48398490
8 1114 0.12523909 0.10811622 0.17104804
9 1642 0.03014575 0.29795320 0.67584853
10 515 0.77180549 0.83819990 0.26298995
11 1877 0.32741508 0.99277109 0.34148083
12 2647 0.38947869 0.43713441 0.21024554
13 845 0.04105275 0.20256457 0.01631959
14 1198 0.36139663 0.96387150 0.37676288
15 2289 0.57097808 0.66038711 0.56230740
16 2009 0.68488024 0.29811683 0.67998461
17 618 0.97111675 0.11926219 0.74538877
18 1076 0.70195881 0.59975160 0.95007272
19 1082 0.01154550 0.12019055 0.16309071
20 2072 0.53553213 0.78843202 0.32475690
21 1610 0.83657146 0.36959607 0.13271604
22 2134 0.80686674 0.95632284 0.63729744
23 1617 0.08093264 0.91357666 0.33092961
24 2248 0.23890930 0.82333634 0.64907957
25 1263 0.96598986 0.31948216 0.30288836
26 518 0.03767233 0.87770033 0.07123327
27 2312 0.91640643 0.80035100 0.66239047
28 2646 0.72622658 0.61135664 0.75960356
29 1650 0.20077621 0.07242114 0.55336017
30 837 0.84020075 0.42158771 0.53927210
31 1467 0.39666235 0.34446560 0.84959232
32 2786 0.39270226 0.75173569 0.65322596
33 1049 0.47255689 0.21875132 0.95088576
34 2863 0.58365691 0.29213397 0.61722305
35 2087 0.35238717 0.35595337 0.49284063
36 2669 0.02847401 0.63196192 0.97600657
37 545 0.99508793 0.89253107 0.49034522
38 1890 0.95755846 0.74403278 0.65517230
39 2969 0.55165118 0.45722242 0.59880179
40 395 0.10195396 0.03609544 0.94756902
41 995 0.23791515 0.56851452 0.36801151
42 2596 0.86009766 0.43901589 0.87818701
43 2334 0.73826129 0.60048445 0.45487507
44 2483 0.49731226 0.95138276 0.49646702
45 1812 0.57992109 0.26943131 0.46061562
46 1476 0.01618339 0.65883839 0.61790820
47 2342 0.47212988 0.07647121 0.60414349
48 2653 0.04238973 0.07128521 0.78587960
49 627 0.46315442 0.37033152 0.55526847
50 925 0.62999477 0.29710220 0.76897834
51 995 0.67324929 0.55107827 0.40428567
52 600 0.08703467 0.36989059 0.51071981
53 711 0.14358380 0.84568953 0.52353644
54 828 0.90847850 0.62079070 0.99279921
55 1776 0.12253259 0.39914002 0.42964742
56 764 0.72886279 0.29966153 0.99601125
57 375 0.95037718 0.38111984 0.78660025
58 694 0.04335591 0.70113494 0.51591063
59 1795 0.01959930 0.94686529 0.50268797
60 638 0.19907246 0.77282832 0.91163748
61 1394 0.50508626 0.21955016 0.26441590
62 1943 0.92638876 0.71611036 0.17385687
63 2882 0.13840169 0.66421796 0.40033126
64 2031 0.16919458 0.70625020 0.53835738
65 1338 0.60662738 0.27962799 0.24496437
66 1077 0.81587669 0.71225050 0.37585096
67 1370 0.84338121 0.66094211 0.58025355
68 1339 0.78807719 0.04101269 0.20895531
69 739 0.01902087 0.06114149 0.80133001
70 2085 0.69808750 0.27976169 0.63880242
71 1240 0.81509312 0.30196772 0.73633076
72 987 0.56840006 0.95661083 0.43881241
73 1720 0.48006288 0.38981872 0.57981238
74 2901 0.16137012 0.37178879 0.25604401
75 1987 0.08925623 0.84314249 0.46371823
76 1876 0.16268237 0.84723500 0.16861486
77 2571 0.02672845 0.31933115 0.61389453
78 2325 0.70962948 0.13250605 0.95810262
79 2503 0.76101818 0.61710912 0.47819473
80 279 0.85747478 0.79130451 0.75115933
81 1381 0.43726582 0.33804871 0.02058322
82 1800 0.41713645 0.90544760 0.17096903
83 2760 0.58564949 0.19755671 0.63996650
84 2949 0.82496758 0.79408518 0.16497848
85 118 0.79313923 0.75460289 0.35472278
86 1736 0.32615257 0.91139485 0.18642647
87 2201 0.95793194 0.32268770 0.89765616
88 750 0.65301961 0.08616947 0.23778386
89 906 0.45867582 0.91120045 0.98494348
90 2202 0.60602188 0.95517383 0.02133074
I want to make a barplot using ggplot2 like this:
In the above-mentioned dataset height should be on the y-axis and S1, S2, S3 should be representing colors of each sample.
I have tried the base R function barplot which gave me the following output. Please give me any suggestion.
barplot(t(as.matrix(examp[,3:5])),col=rainbow(3))
It's not clear to me exactly what you want to plot. You say you want height on the y axis, but the examples you show are all 'filled to the top', implying the same height for each ID. Also, it is not clear what the numbers associated with each sample represent. I am guessing they should be relative weightings for the bar heights.
Assuming you actually want a filled bar plot as in the examples, with the relative sizes of the bars dictated by the sample values, you can do:
library(tidyr)
library(dplyr)
library(ggplot2)
df %>%
mutate(ID = reorder(ID, S3/(S3 + S2 + S1))) %>%
pivot_longer(3:5, names_to = "Sample", values_to = "Value") %>%
ggplot(aes(ID, Value * height, fill = Sample)) +
geom_col(position = "fill", color = NA) +
labs(y = "Height") +
theme_classic() +
scale_fill_manual(values = c("red", "green", "blue"))
Alternative
df %>%
arrange(order(height)) %>%
group_by(height) %>%
summarize(across(everything(), mean)) %>%
pivot_longer(3:5, names_to = "Sample", values_to = "Value") %>%
ggplot(aes(height, Value, fill = Sample, colour = Sample)) +
geom_smooth(method = loess, formula = y ~ x, linetype = 2, alpha = 0.2) +
theme_bw()

Change axises' scale in a plot without creating new varibale

I have a dataset like below (this is only the first 20 rows and the first 3 columns of data):
row fitted measured
1 1866 1950
2 2489 2500
3 1486 1530
4 1682 1720
5 1393 1402
6 2524 2645
7 2676 2789
8 3200 3400
9 1455 1456
10 1685 1765
11 2587 2597
12 3040 3050
13 2767 2769
14 3300 3310
15 4001 4050
16 1918 2001
17 2889 2907
18 2063 2150
19 1591 1640
20 3578 3601
I plotted this data
plot(data$measured~data$fitted, ylab = expression("Measured Length (" * mu ~ "m)"),
xlab = expression("NIR Fitted Length (" * mu ~ "m)"), cex.lab=1.5, cex.axis=1.5)
and got the following:
As you can see the axises scales are in micrometer, I need the axis to be in millimeter.
How can I plot the data while axises are in millimeter, WITHOUT creating a new variable?
Like this;
If I want to create a new variable, I have to change the whole 2000 lines code that I've written before and that's not a road that I want to go! :|
Thanks much :)
I used #bdemarest method for plot and #IukeA method for abline ;
plot(y=data$measured/1000,x=data$fitted/1000, ylab = expression("Measured Length (mm)"),
xlab = expression("NIR Fitted Length (mm)"), cex.lab=1.5, cex.axis=1.5)
a = lm(I(data$measured/1000)~I(data$fitted/1000), data=data)
abline(a)
Here is the final plot;

Interpolate quarterly to daily time series in a matrix

I have read the following post, as well as and related ones, and I have found it really useful:
Interpolate / Extend quarterly to monthly series
I have a similar but more general/extended problem, which I still haven't figure how to solve. Got a matrix of seven time series (named value1, ..., value7, below) including quarterly data for 63 dates, as well as NAs.
> str(test)
'data.frame': 63 obs. of 8 variables:
$ Date : Date, format: "2001-03-30" "2001-06-29" "2001-09-28" ...
$ value1: num 320 181.1 19.7 133.1 160.6 ...
$ value2: num 4741 4556 4115 3892 3605 ...
$ value3: num 146.8 -163.9 73.2 111.6 210.5 ...
$ value4: num -135 -383.3 104.3 74.7 -75.4 ...
$ value5: num 21.6 20.2 NA NA NA ...
$ value6: num -19.1 -82.4 85 134.6 111 ...
$ value7: num -163 -215 -164 -137 -199 ...
> test
Date value1 value2 value3 value4 value5 value6 value7
1 2001-03-30 319.952 4740.905 146.756 -134.998 21.645 -19.0611 -162.713
2 2001-06-29 181.103 4555.732 -163.867 -383.334 20.199 -82.3660 -215.105
3 2001-09-28 19.724 4115.053 73.189 104.300 NA 84.9740 -164.073
4 2001-12-31 133.134 3891.754 111.567 74.683 NA 134.6460 -136.974
5 2002-03-28 160.564 3605.080 210.533 -75.351 NA 110.9770 -199.083
6 2002-06-28 -111.902 3220.115 -107.759 -22.624 NA 408.4770 -172.327
7 2002-09-30 -127.751 2962.472 -93.616 241.749 NA 687.2240 -195.772
8 2002-12-31 -59.553 2697.029 -98.068 119.288 NA 903.8211 -137.965
9 2003-03-31 86.427 2509.511 -78.662 -124.428 NA 1130.9380 -180.496
10 2003-06-30 90.070 2554.473 -14.345 -66.764 NA 925.9010 -103.080
11 2003-09-30 246.801 3000.005 0.001 -244.487 NA 1005.6370 -123.959
12 2003-12-31 325.088 3519.168 388.592 129.915 NA 739.5460 -162.781
13 2004-03-31 359.263 4041.043 206.260 -101.966 NA 745.8810 -202.047
14 2004-06-30 367.347 4657.622 254.678 -59.913 NA 852.4181 -360.963
15 2004-09-30 373.089 4943.322 263.395 -37.116 NA 857.8670 -406.748
16 2004-12-31 351.817 5001.434 362.188 118.842 NA 663.5370 -470.379
17 2005-03-31 287.224 4991.632 251.327 39.029 24.245 785.3220 -518.472
18 2005-06-30 311.324 4989.710 265.163 11.546 25.653 676.1650 -303.265
19 2005-09-30 369.478 5273.006 429.086 133.030 30.615 667.2330 -362.296
20 2005-12-30 482.974 5847.577 537.279 63.616 24.447 -265.5200 -329.140
21 2006-03-31 432.157 5953.107 566.349 196.971 -4.915 -1807.2560 -310.326
22 2006-06-30 295.014 5909.556 218.850 -6.842 -17.449 -1837.8140 -455.364
23 2006-09-29 318.926 5714.423 230.185 14.135 -13.551 -1667.5960 -424.892
24 2006-12-29 232.784 5649.147 271.616 142.736 46.000 2256.0000 -666.418
25 2007-03-30 -190.000 5549.989 41.000 373.000 62.000 2674.0000 -586.000
26 2007-06-29 -70.000 5642.622 -635.000 -412.000 80.000 3943.0000 -414.000
27 2007-09-28 153.000 5873.000 223.000 168.000 76.000 3807.0000 -419.000
28 2007-12-31 234.000 5858.000 61.000 -153.000 76.000 3380.0000 -266.000
29 2008-03-31 83.000 6112.000 16.000 110.000 86.000 3534.0000 -323.000
30 2008-06-30 -18.000 6165.000 -242.000 -82.000 91.000 3694.0000 -106.000
31 2008-09-30 426.000 6404.000 -216.000 -497.000 87.000 3799.0000 -82.000
32 2008-12-31 -237.000 5808.000 -250.000 110.000 88.000 3680.0000 -113.000
33 2009-03-31 -18.000 5498.000 -391.000 -252.000 94.000 2844.0000 -84.000
34 2009-06-30 33.000 5320.000 -144.000 -120.000 102.000 3107.0000 -112.000
35 2009-09-30 205.000 4919.000 -142.000 -288.000 110.000 3059.0000 -97.000
36 2009-12-31 1572.000 5403.000 1150.000 -361.000 116.000 1884.0000 -174.000
37 2010-03-31 282.000 5800.000 23.000 -237.000 46.000 672.0000 -48.000
38 2010-06-30 221.000 6269.000 -98.000 -279.000 52.000 684.0000 -31.000
39 2010-09-30 217.000 6491.000 -124.000 -343.000 53.000 671.0000 -31.000
40 2010-12-31 511.000 6494.000 -213.000 -647.000 37.000 632.0000 -38.000
41 2011-03-31 142.000 6533.000 -168.000 -326.000 45.000 485.0000 -38.000
42 2011-06-30 185.000 6454.000 174.000 17.000 45.000 338.0000 -67.000
43 2011-09-30 217.000 6526.000 189.000 -5.000 39.000 203.0000 -58.000
44 2011-12-30 140.000 6568.000 187.000 63.000 41.000 102.0000 -87.000
45 2012-03-30 -517.000 6540.000 107.000 384.000 41.000 306.0000 -40.000
46 2012-06-29 142.000 6379.000 81.000 -49.000 41.000 262.0000 -39.000
47 2012-09-28 -65.000 5958.000 -240.000 -185.000 42.000 560.0000 -32.000
48 2012-12-31 -356.000 5422.000 -286.000 82.000 43.000 859.0000 -22.000
49 2013-03-28 -32.000 4925.000 -155.000 -159.000 43.000 861.0000 -20.000
50 2013-06-28 30.000 4673.000 -35.000 -8.000 40.000 930.0000 -28.000
51 2013-09-30 152.000 4865.000 21.000 -61.000 46.000 868.0000 -15.000
52 2013-12-31 189.000 5299.000 21.000 -128.000 43.000 871.0000 -21.000
53 2014-03-31 102.000 5608.000 -204.000 -277.000 46.000 1156.0000 -21.000
54 2014-06-30 116.000 5888.000 -28.000 -118.000 46.000 1262.0000 -23.000
55 2014-09-30 112.000 5856.000 18.000 -65.000 42.000 1270.0000 -29.000
56 2014-12-31 -282.000 5506.000 116.000 170.000 40.000 1172.0000 -22.000
57 2015-03-31 -91.000 5139.000 -172.000 -129.000 40.000 1362.0000 -22.000
58 2015-06-30 -92.000 4640.000 -57.000 55.000 NA 1440.0000 -17.000
59 2015-09-30 -116.000 4272.000 -59.000 64.000 NA 1505.0000 -25.000
60 2015-12-31 -15.000 3991.000 53.000 112.000 NA 1477.0000 -32.000
61 2016-03-31 -35.000 3793.000 -42.000 19.000 NA 1520.0000 -26.000
62 2016-06-30 25.000 3878.000 -85.000 -67.000 NA 1281.0000 -21.000
63 2016-09-30 -260.000 4124.000 29.000 67.000 NA 374.0000 -9.000
I want to interpolate daily values (output will include 5664 days in total), using a cubic spline or linear relation. The solution provided in the link above is good but it works only if I apply it on each time series separately, to which I always need to associate the "Date" column: (Date, value1); (Date, value2); ..., as below, which is quite time-consuming:
DateSeq <- seq(test$Date[1],tail(test$Date,1),by="1 day")
test1 <- test[1:2]
test2 <- test[c(1,3)]
...
test1Daily <- data.frame(test=DateSeq, Interp.Value=spline(test1, method="natural", xout=DateSeq)$y)
test2Daily <- data.frame(test=DateSeq, Interp.Value=spline(test2, method="natural", xout=DateSeq)$y)
...
merge1 <- merge(test1, testDaily1, by='Date', all.y = T)
merge2 <- merge(test2, testDaily2, by='Date', all.y = T)
...
...then finally merge all the merged variables above.
Does anyone knows how to apply the interpolation once to the whole matrix (meaning to each column, or time series)?
Many thanks in advance.
I have found the following solution, and the way to plot it to verify that things work out well. Hope it may be useful for others!
test_z1 <- zoo(test, order.by = test$Date, frequency = 1)
test_t1 <- as.ts(x=test_z1)
test_t2 <- as.zoo(test_t1)
index(test_t2) <- as.Date(index(test_t2), origin = '1970-01-01')
test_t2_ncol <- test_t2[,-c(1)]
test_g <- na.spline(test_t2_ncol)
Now I put together each time series ("value1, value2,...", in "test") with its own interpolated time series in "test_g", and plot them to verify by eye the goodness of the interpolation:
interp_val1 <- test_g[,-c(2:7)]
orig_val1 <- test[,-c(3:8)]
orig_val1_z <- read.zoo(orig_val1)
merge_val1 <- merge(orig_val1_z, interp_val1)
options(stringsAsFactors = FALSE) # to avoid conversion to factors
merge_val1_df <- data.frame(Date=time(merge_val1), merge_val1, check.names=FALSE, row.names=NULL)
plot(merge_val1_df$orig_val1_z, lwd=2)
lines(merge_val1_df$interp_val1, lwd=1, col="green")
It seems that the interpolation works well!

R matplot function

As I am beginner R, I allow myself to ask R users a little question.
I want to represent in a graphic (points, lines, curves) the values of weight of two human groups treated and not treated by drug (0,1) measured ten times (months).
drug NumberIndividu Mar Apr May June July August September November October December
1 9 25.92 24.6 31.85 38.50 53.70 53.05 65.65 71.45 69.10 67.20
1 10 28.10 26.6 32.00 38.35 53.60 53.25 65.35 65.95 67.80 65.95
1 11 29.10 28.8 30.80 38.10 52.25 47.30 62.20 68.05 66.20 67.55
1 13 27.16 25.0 27.15 34.85 47.30 43.85 54.65 62.25 60.85 58.05
0 5 25.89 25.2 26.50 27.45 37.05 38.95 43.30 50.60 48.20 50.10
0 6 28.19 27.6 28.05 28.60 36.15 37.20 40.40 47.80 45.25 44.85
0 7 28.06 27.2 27.45 28.85 39.20 41.80 51.40 57.10 54.55 55.30
0 8 22.39 21.2 30.10 30.90 42.95 46.30 48.15 54.85 53.35 49.90
I tried :
w= read.csv (file="/file-weight.csv", header=TRUE, sep=",")
w<-data.frame(w)
rownames(w[1:8,])
rownames(w)<-(c(w[,1]))
cols <- character(nrow(w))
cols[rownames(w) %in% c(rownames(w[1:4,]))]<-"blue"
cols[rownames(w) %in% c(rownames(w[5:8,]))]<-"red"
pairs(w,col=cols)
My question is how to configurate matplot function to have one graphic view (points or curves or hist +curves)
My main goal is to visualize all distributions of individus following two colors of first column (drug) for all dates in one image.
Thanks a lot for your suggestions
Is this what you had in mind?
The code is based on the answer to ->this question<-, just using your dataset (df) instead of iris. So in that response, replace:
x <- with(iris, data.table(id=1:nrow(iris), group=Species, Sepal.Length, Sepal.Width,Petal.Length, Petal.Width))
with:
xx <- with(df, data.table(id=1:nrow(df), group=drug, df[3:12]))
If all you want is the density plots/histograms, it's easier (see below). These are complementary, because they show that weight is increasing in both control and test groups, just faster in the test group. You wouldn't pick that up from the scatterplot matrix. Also, there's the suggestion that variability in weight is greater in the control group, and grows over time.
library(ggplot2)
library(reshape2) # for melt(...)
# convert df into a form suitable to use with ggplot
gg <- melt(df,id=1:2, variable.name="Month", value.name="Weight")
# density plots
ggplot(gg) +
stat_density(aes(x=Weight, y=..scaled.., color=factor(drug)),geom="line", position="dodge")+
facet_grid(Month~.)+
scale_color_discrete("Drug")
# histograms
ggplot(gg) +
geom_histogram(aes(x=Weight, fill=factor(drug)), position="dodge")+
facet_grid(Month~.)+
scale_fill_discrete("Drug")

Given data points and y value, give x value

Given a set of (x,y) coordinates, how can I solve for x, from y. If you were to plot the coordinates, they would be non-linear, but pretty close to exponential. I tried approx(), but it is way off. Here is example data. In this scenario, how could I solve for y == 50?
V1 V3
1 5.35 11.7906
2 10.70 15.0451
3 16.05 19.4243
4 21.40 20.7885
5 26.75 22.0584
6 32.10 25.4367
7 37.45 28.6701
8 42.80 30.7500
9 48.15 34.5084
10 53.50 37.0096
11 58.85 39.3423
12 64.20 41.5023
13 69.55 43.4599
14 74.90 44.7299
15 80.25 46.5738
16 85.60 47.7548
17 90.95 49.9749
18 96.30 51.0331
19 101.65 52.0207
20 107.00 52.9781
21 112.35 53.8730
22 117.70 54.2907
23 123.05 56.3025
24 128.40 56.6949
25 133.75 57.0830
26 139.10 58.5051
27 144.45 59.1440
28 149.80 60.0687
29 155.15 60.6627
30 160.50 61.2313
31 165.85 61.7748
32 171.20 62.5587
33 176.55 63.2684
34 181.90 63.7085
35 187.25 64.0788
36 192.60 64.5807
37 197.95 65.2233
38 203.30 65.5331
39 208.65 66.1200
40 214.00 66.6208
41 219.35 67.1952
42 224.70 67.5270
43 230.05 68.0175
44 235.40 68.3869
45 240.75 68.7485
46 246.10 69.1878
47 251.45 69.3980
48 256.80 69.5899
49 262.15 69.7382
50 267.50 69.7693
51 272.85 69.7693
52 278.20 69.7693
53 283.55 69.7693
54 288.90 69.7693
I suppose the problem you have is that approx solves for y given x, while you are talking about solving for x given y. So you need to switch your variables x and y when using approx:
df <- read.table(textConnection("
V1 V3
85.60 47.7548
90.95 49.9749
96.30 51.0331
101.65 52.0207
"), header = TRUE)
approx(x = df$V3, y = df$V1, xout = 50)
# $x
# [1] 50
#
# $y
# [1] 91.0769
Also, if y is exponential with respect to x, then you have a linear relationship between x and log(y), so it makes more sense to use a linear interpolator between x and log(y), then take the exponential to get back to y:
exp(approx(x = df$V3, y = log(df$V1), xout = 50)$y)
# [1] 91.07339

Resources