How do I save fitted models in R? - r

I've fitted a regression tree. I've tried to save the fitted model with the save() function but if I close R, load the object and recall it I get a different output.
Here's the code:
training.set=iris[,-5]
library(tree)
set.seed(123)
part1 = sample(1:nrow(training.set), round(nrow(training.set)/2))
part2 = setdiff(1:nrow(training.set), part1)
tree.output = tree("Sepal.Length~.", data=training.set[part1,],
control=tree.control(nobs=length(part1), minsize=2, mindev=0.001))
prune.t = prune.tree(tree.output, newdata=training.set[part2,])
plot(prune.t)
J = prune.t$size[prune.t$dev == min(prune.t$dev)]
J
m.tree = prune.tree(tree.output,best=J)
save(m.tree,file="my model.RData")
m.tree
Output:
node), split, n, deviance, yval
* denotes terminal node
1) root 75 49.54000 5.756
2) Petal.Length < 4.3 41 6.69800 5.161
4) Petal.Length < 3.55 31 3.82800 5.019
8) Sepal.Width < 3.45 17 1.04900 4.794
16) Petal.Length < 1.45 9 0.52220 4.644 *
17) Petal.Length > 1.45 8 0.09875 4.962 *
9) Sepal.Width > 3.45 14 0.86930 5.293
18) Sepal.Width < 3.85 8 0.17880 5.138 *
19) Sepal.Width > 3.85 6 0.24000 5.500 *
5) Petal.Length > 3.55 10 0.32000 5.600 *
3) Petal.Length > 4.3 34 10.83000 6.474
6) Petal.Length < 5.7 28 3.46400 6.264
12) Petal.Width < 2.05 23 2.42600 6.187 *
13) Petal.Width > 2.05 5 0.26800 6.620 *
7) Petal.Length > 5.7 6 0.41500 7.450
14) Petal.Length < 6.35 4 0.02750 7.275 *
15) Petal.Length > 6.35 2 0.02000 7.800 *
Second part of the script:
q()
rm(list=ls() )
load("my model.RData")
m.tree
Output:
$frame
var n dev yval splits.cutleft splits.cutright
1 Petal.Length 75 49.5448000 5.756000 <4.3 >4.3
2 Petal.Length 41 6.6975610 5.160976 <3.55 >3.55
4 Sepal.Width 31 3.8283871 5.019355 <3.45 >3.45
8 Petal.Length 17 1.0494118 4.794118 <1.45 >1.45
16 <leaf> 9 0.5222222 4.644444
17 <leaf> 8 0.0987500 4.962500
9 Sepal.Width 14 0.8692857 5.292857 <3.85 >3.85
18 <leaf> 8 0.1787500 5.137500
19 <leaf> 6 0.2400000 5.500000
5 <leaf> 10 0.3200000 5.600000
3 Petal.Length 34 10.8261765 6.473529 <5.7 >5.7
6 Petal.Width 28 3.4642857 6.264286 <2.05 >2.05
12 <leaf> 23 2.4260870 6.186957
13 <leaf> 5 0.2680000 6.620000
7 Petal.Length 6 0.4150000 7.450000 <6.35 >6.35
14 <leaf> 4 0.0275000 7.275000
15 <leaf> 2 0.0200000 7.800000
$where
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
8 17 6 16 13 5 13 13 13 10 13 13 6 14 5 13 9 9 13 16 14
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
10 10 13 10 10 10 13 5 8 13 16 13 14 5 13 13 6 13 6 9 5
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
8 6 9 9 14 5 8 13 8 17 14 6 10 8 10 13 16 6 10 5 9
24 93 39 69 124 66 112 148 50 56 1 37
6 10 5 13 13 13 13 13 5 13 8 8
$terms
Sepal.Length ~ Sepal.Width + Petal.Length + Petal.Width
attr(,"variables")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"factors")
Sepal.Width Petal.Length Petal.Width
Sepal.Length 0 0 0
Sepal.Width 1 0 0
Petal.Length 0 1 0
Petal.Width 0 0 1
attr(,"term.labels")
[1] "Sepal.Width" "Petal.Length" "Petal.Width"
attr(,"order")
[1] 1 1 1
attr(,"intercept")
[1] 1
attr(,"response")
[1] 1
attr(,".Environment")
<environment: 0x00000000059480f0>
attr(,"predvars")
list(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width)
attr(,"dataClasses")
Sepal.Length Sepal.Width Petal.Length Petal.Width
"numeric" "numeric" "numeric" "numeric"
$call
snip.tree(tree = tree.output, nodes = c(19L, 18L, 5L, 16L, 13L,
12L))
$y
44 118 61 130 138 7 77 128 79 65 134 64 94 142 14 122 33 6 150 126 116
5.0 7.7 5.0 7.2 6.4 4.6 6.8 6.1 6.0 5.6 6.3 6.1 5.0 6.9 4.3 5.6 5.2 5.4 5.9 7.2 6.4
90 82 127 83 89 68 74 36 18 147 108 143 146 3 55 87 25 135 26 16 46
5.5 5.5 6.2 5.8 5.6 5.8 6.1 5.0 5.1 6.3 7.3 5.8 6.7 4.7 6.5 6.7 4.8 6.1 5.0 5.7 4.8
45 40 17 15 113 48 28 114 5 132 137 12 54 20 97 71 131 35 60 9 34
5.1 5.1 5.4 5.8 6.8 4.6 5.2 5.7 5.0 7.9 6.3 4.8 5.5 5.1 5.7 5.9 7.4 4.9 5.2 4.4 5.5
24 93 39 69 124 66 112 148 50 56 1 37
5.1 5.8 4.4 6.2 6.3 6.7 6.4 6.5 5.0 5.7 5.1 5.5
$weights
[1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
[41] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
attr(,"class")
[1] "tree"
attr(,"xlevels")
attr(,"xlevels")$Sepal.Width
NULL
attr(,"xlevels")$Petal.Length
NULL
attr(,"xlevels")$Petal.Width
NULL
Do I need to use a different function to save the model?
Thank you.

Your object has been stored correctly, however, it is being printed out differently. The object m.tree is a list with class tree:
R> class(m.tree)
[1] "tree"
When you print the object, it looks for the function print.tree (vis the generic print function). This function is only available after you load the tree package.
library(tree)
load("my model.RData")
m.tree
will give you what you want.
If you are interested, run
getS3method("print", "tree")
to see the print method.

Related

Add new column to state data frame based on other column data [duplicate]

This question already has answers here:
Categorize numeric variable into group/ bins/ breaks
(4 answers)
Closed 1 year ago.
I am attempting to add a new column to the state sample data frame in R. I am hoping for this column to cluster the ID of states into broader categories (1-4). My code is close to what I am looking for but I am not getting it quite right.. I know I could enter each state ID line by line but is there a a quicker way? Thank you!
library(tidyverse)
#Add column to denote each state
States=state.x77
States=data.frame(States)
States <- tibble::rowid_to_column(States, "ID")
States
#Create new variable for state buckets
States <- States %>%
mutate(WAGE_BUCKET=case_when(ID <= c(1,12) ~ '1',
ID <= c(13,24) ~ '2',
ID <= c(25,37) ~ '3',
ID <= c(38,50) ~ '4',
TRUE ~ 'NA'))
View(States) #It is not grouping the states in the way I want/I am still getting some NA values but unsure why!
You can use cut or findInterval if all of your groups will be using contiguous ID values:
findInterval(States$ID, c(0, 12, 24, 37, 51))
# [1] 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4
If you want to make it a bit more verbose, you can use dplyr::between in your case_when:
States %>%
mutate(
WAGE_BUCKET = case_when(
between(ID, 1, 12) ~ "1",
between(ID, 13, 24) ~ "2",
between(ID, 25, 37) ~ "3",
between(ID, 38, 50) ~ "4",
TRUE ~ NA_character_)
)
# ID Population Income Illiteracy Life Exp Murder HS Grad Frost Area WAGE_BUCKET
# 1 1 3615 3624 2.1 69.05 15.1 41.3 20 50708 1
# 2 2 365 6315 1.5 69.31 11.3 66.7 152 566432 1
# 3 3 2212 4530 1.8 70.55 7.8 58.1 15 113417 1
# 4 4 2110 3378 1.9 70.66 10.1 39.9 65 51945 1
# 5 5 21198 5114 1.1 71.71 10.3 62.6 20 156361 1
# 6 6 2541 4884 0.7 72.06 6.8 63.9 166 103766 1
# 7 7 3100 5348 1.1 72.48 3.1 56.0 139 4862 1
# 8 8 579 4809 0.9 70.06 6.2 54.6 103 1982 1
# 9 9 8277 4815 1.3 70.66 10.7 52.6 11 54090 1
# 10 10 4931 4091 2.0 68.54 13.9 40.6 60 58073 1
# 11 11 868 4963 1.9 73.60 6.2 61.9 0 6425 1
# 12 12 813 4119 0.6 71.87 5.3 59.5 126 82677 1
# 13 13 11197 5107 0.9 70.14 10.3 52.6 127 55748 2
# 14 14 5313 4458 0.7 70.88 7.1 52.9 122 36097 2
# 15 15 2861 4628 0.5 72.56 2.3 59.0 140 55941 2
# 16 16 2280 4669 0.6 72.58 4.5 59.9 114 81787 2
# 17 17 3387 3712 1.6 70.10 10.6 38.5 95 39650 2
# 18 18 3806 3545 2.8 68.76 13.2 42.2 12 44930 2
# 19 19 1058 3694 0.7 70.39 2.7 54.7 161 30920 2
# 20 20 4122 5299 0.9 70.22 8.5 52.3 101 9891 2
# 21 21 5814 4755 1.1 71.83 3.3 58.5 103 7826 2
# 22 22 9111 4751 0.9 70.63 11.1 52.8 125 56817 2
# 23 23 3921 4675 0.6 72.96 2.3 57.6 160 79289 2
# 24 24 2341 3098 2.4 68.09 12.5 41.0 50 47296 2
# 25 25 4767 4254 0.8 70.69 9.3 48.8 108 68995 3
# 26 26 746 4347 0.6 70.56 5.0 59.2 155 145587 3
# 27 27 1544 4508 0.6 72.60 2.9 59.3 139 76483 3
# 28 28 590 5149 0.5 69.03 11.5 65.2 188 109889 3
# 29 29 812 4281 0.7 71.23 3.3 57.6 174 9027 3
# 30 30 7333 5237 1.1 70.93 5.2 52.5 115 7521 3
# 31 31 1144 3601 2.2 70.32 9.7 55.2 120 121412 3
# 32 32 18076 4903 1.4 70.55 10.9 52.7 82 47831 3
# 33 33 5441 3875 1.8 69.21 11.1 38.5 80 48798 3
# 34 34 637 5087 0.8 72.78 1.4 50.3 186 69273 3
# 35 35 10735 4561 0.8 70.82 7.4 53.2 124 40975 3
# 36 36 2715 3983 1.1 71.42 6.4 51.6 82 68782 3
# 37 37 2284 4660 0.6 72.13 4.2 60.0 44 96184 3
# 38 38 11860 4449 1.0 70.43 6.1 50.2 126 44966 4
# 39 39 931 4558 1.3 71.90 2.4 46.4 127 1049 4
# 40 40 2816 3635 2.3 67.96 11.6 37.8 65 30225 4
# 41 41 681 4167 0.5 72.08 1.7 53.3 172 75955 4
# 42 42 4173 3821 1.7 70.11 11.0 41.8 70 41328 4
# 43 43 12237 4188 2.2 70.90 12.2 47.4 35 262134 4
# 44 44 1203 4022 0.6 72.90 4.5 67.3 137 82096 4
# 45 45 472 3907 0.6 71.64 5.5 57.1 168 9267 4
# 46 46 4981 4701 1.4 70.08 9.5 47.8 85 39780 4
# 47 47 3559 4864 0.6 71.72 4.3 63.5 32 66570 4
# 48 48 1799 3617 1.4 69.48 6.7 41.6 100 24070 4
# 49 49 4589 4468 0.7 72.48 3.0 54.5 149 54464 4
# 50 50 376 4566 0.6 70.29 6.9 62.9 173 97203 4
It is a vector of length > 1. The comparison operators works on a single vector. We could use between
library(dplyr)
States <- States %>%
mutate(WAGE_BUCKET=case_when(between(ID, 1, 12) ~ '1',
between(ID, 13,24) ~ '2',
between(ID, 25,37) ~ '3',
between(ID, 38,50) ~ '4',
TRUE ~ NA_character_))
Or another option is to use & with > and <=
States %>%
mutate(WAGE_BUCKET=case_when(ID >= 1 & ID <=12 ~ '1',
ID >= 13 & ID <= 24) ~ '2',
ID >= 25 & ID <= 37 ~ '3',
ID >= 38 & ID <= 50 ~ '4',
TRUE ~ NA_character))
Or may be the OP meant to use %in%
States %>%
mutate(WAGE_BUCKET=case_when(ID %in% c(1,12) ~ '1',
ID %in% c(13,24) ~ '2',
ID %in% c(25,37) ~ '3',
ID %in% c(38,50) ~ '4',
TRUE ~ NA_character_))

replace NA with 0 and all other values/text as 1

airquality
Ozone Solar.R Wind Temp Month Day
1 41 190 7.4 67 5 1
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
5 NA NA 14.3 56 5 5
6 28 NA 14.9 66 5 6
7 23 299 8.6 65 5 7
8 19 99 13.8 59 5 8
9 8 19 20.1 61 5 9
Hi there,
How do I replace values in Ozone to be binary? If NA then 0 and if a value then 1.
Thanks
H
Assuming your dataframe is called airquality
airquality$Ozone <- ifelse(is.na(airquality$Ozone), 0, 1)
airquality$Ozone <- as.integer(!is.na(airquality$Ozone))
Alternatively
airquality$Ozone[!is.na(airquality$Ozone)] <- 1L
airquality$Ozone[is.na(airquality$Ozone)] <- 0L

Wrong Fit using nls function

When I try to fit an exponential decay and my x axis has decimal number, the fit is never correct. Here's my data below:
exp.decay = data.frame(time,counts)
time counts
1 0.4 4458
2 0.6 2446
3 0.8 1327
4 1.0 814
5 1.2 549
6 1.4 401
7 1.6 266
8 1.8 182
9 2.0 140
10 2.2 109
11 2.4 83
12 2.6 78
13 2.8 57
14 3.0 50
15 3.2 31
16 3.4 22
17 3.6 23
18 3.8 20
19 4.0 19
20 4.2 9
21 4.4 7
22 4.6 4
23 4.8 6
24 5.0 4
25 5.2 6
26 5.4 2
27 5.6 7
28 5.8 2
29 6.0 0
30 6.2 3
31 6.4 1
32 6.6 1
33 6.8 2
34 7.0 1
35 7.2 2
36 7.4 1
37 7.6 1
38 7.8 0
39 8.0 0
40 8.2 0
41 8.4 0
42 8.6 1
43 8.8 0
44 9.0 0
45 9.2 0
46 9.4 1
47 9.6 0
48 9.8 0
49 10.0 1
fit.one.exp <- nls(counts ~ A*exp(-k*time),data=exp.decay, start=c(A=max(counts),k=0.1))
plot(exp.decay, col='darkblue',xlab = 'Track Duration (seconds)',ylab = 'Number of Particles', main = 'Exponential Fit')
lines(predict(fit.one.exp), col = 'red', lty=2, lwd=2)
I always get this weird fit. Seems to me that the fit is not recognizing the right x axis, because when I use a different set of data, with only integers in the x axis (time) the fit works! I don't understand why it's different with different units.
You need one small modification:
lines(predict(fit.one.exp), col = 'red', lty=2, lwd=2)
should be
lines(exp.decay$time, predict(fit.one.exp), col = 'red', lty=2, lwd=2)
This way you make sure to plot against the desired values on your abscissa.
I tested it like this:
data = read.csv('exp_fit_r.csv')
A0 <- max(data$count)
k0 <- 0.1
fit <- nls(data$count ~ A*exp(-k*data$time), start=list(A=A0, k=k0), data=data)
plot(data)
lines(data$time, predict(fit), col='red')
which gives me the following output:
As you can see, the fit describes the actual data very well, it was just a matter of plotting against the correct abscissa values.

How to flatten out nested list into one list more efficiently instead of using unlist method?

I have a nested list which contains set of data.frame objects in it, now I want them flatten out. I used most common approach like unlist method, it is not properly fatten out my list, the output was not well represented. How can I make this happen more efficiently? Does anyone knows any trick of doing this operation? Thanks.
example:
mylist <- list(pass=list(Alpha.df1_yes=airquality[2:4,], Alpha.df2_yes=airquality[3:6,],Alpha.df3_yes=airquality[2:5,],Alpha.df4_yes=airquality[7:9,]),
fail=list(Alpha.df1_no=airquality[5:7,], Alpha.df2_no=airquality[8:10,], Alpha.df3_no=airquality[13:16,],Alpha.df4_no=airquality[11:13,]))
I tried like this, it works but output was not properly arranged.
res <- lapply(mylist, unlist)
after flatten out, I would like to do merge them without duplication:
out <- lapply(res, rbind.data.frame)
my desired output:
mylist[[1]]$pass:
Ozone Solar.R Wind Temp Month Day
2 36 118 8.0 72 5 2
3 12 149 12.6 74 5 3
4 18 313 11.5 62 5 4
How can make this sort of flatten output more compatibly represented? Can anyone propose possible idea of doing this in R? Thanks a lot.
Using lapply and duplicated:
res <- lapply(mylist, function(i){
x <- do.call(rbind, i)
x[ !duplicated(x), ]
rownames(x) <- NULL
x
})
res$pass
# Ozone Solar.R Wind Temp Month Day
# 1 36 118 8.0 72 5 2
# 2 12 149 12.6 74 5 3
# 3 18 313 11.5 62 5 4
# 4 12 149 12.6 74 5 3
# 5 18 313 11.5 62 5 4
# 6 NA NA 14.3 56 5 5
# 7 28 NA 14.9 66 5 6
# 8 36 118 8.0 72 5 2
# 9 12 149 12.6 74 5 3
# 10 18 313 11.5 62 5 4
# 11 NA NA 14.3 56 5 5
# 12 23 299 8.6 65 5 7
# 13 19 99 13.8 59 5 8
# 14 8 19 20.1 61 5 9
Above still returns a list, if we want to keep all in one dataframe with no lists, then:
res <- do.call(rbind, unlist(mylist, recursive = FALSE))
res <- res[!duplicated(res), ]
res
# Ozone Solar.R Wind Temp Month Day
# pass.Alpha.df1_yes.2 36 118 8.0 72 5 2
# pass.Alpha.df1_yes.3 12 149 12.6 74 5 3
# pass.Alpha.df1_yes.4 18 313 11.5 62 5 4
# pass.Alpha.df2_yes.5 NA NA 14.3 56 5 5
# pass.Alpha.df2_yes.6 28 NA 14.9 66 5 6
# pass.Alpha.df4_yes.7 23 299 8.6 65 5 7
# pass.Alpha.df4_yes.8 19 99 13.8 59 5 8
# pass.Alpha.df4_yes.9 8 19 20.1 61 5 9
# fail.Alpha.df2_no.10 NA 194 8.6 69 5 10
# fail.Alpha.df3_no.13 11 290 9.2 66 5 13
# fail.Alpha.df3_no.14 14 274 10.9 68 5 14
# fail.Alpha.df3_no.15 18 65 13.2 58 5 15
# fail.Alpha.df3_no.16 14 334 11.5 64 5 16
# fail.Alpha.df4_no.11 7 NA 6.9 74 5 11
# fail.Alpha.df4_no.12 16 256 9.7 69 5 12

R ggplot2 legend inside the figure

So, I have the following data.frame, and I want to generate two plots in one graph for yval vs. xval, for each zval and type tp. The lef
> df
xval yval se zval cond
1 1.0 1.831564e-02 1.831564e-03 0 a
2 1.2 2.705185e-02 2.705185e-03 0 a
3 1.4 3.916390e-02 3.916390e-03 0 a
4 1.6 5.557621e-02 5.557621e-03 0 a
5 1.8 7.730474e-02 7.730474e-03 0 a
6 2.0 1.053992e-01 1.053992e-02 0 a
7 2.2 1.408584e-01 1.408584e-02 0 a
8 2.4 1.845195e-01 1.845195e-02 0 a
9 2.6 2.369278e-01 2.369278e-02 0 a
10 2.8 2.981973e-01 2.981973e-02 0 a
11 3.0 3.678794e-01 3.678794e-02 0 a
12 3.2 4.448581e-01 4.448581e-02 0 a
13 3.4 5.272924e-01 5.272924e-02 0 a
14 3.6 6.126264e-01 6.126264e-02 0 a
15 3.8 6.976763e-01 6.976763e-02 0 a
16 4.0 7.788008e-01 7.788008e-02 0 a
17 4.2 8.521438e-01 8.521438e-02 0 a
18 4.4 9.139312e-01 9.139312e-02 0 a
19 4.6 9.607894e-01 9.607894e-02 0 a
20 4.8 9.900498e-01 9.900498e-02 0 a
21 5.0 1.000000e+00 1.000000e-01 0 a
22 5.2 9.900498e-01 9.900498e-02 0 a
23 5.4 9.607894e-01 9.607894e-02 0 a
24 5.6 9.139312e-01 9.139312e-02 0 a
25 5.8 8.521438e-01 8.521438e-02 0 a
26 6.0 7.788008e-01 7.788008e-02 0 a
27 6.2 6.976763e-01 6.976763e-02 0 a
28 6.4 6.126264e-01 6.126264e-02 0 a
29 6.6 5.272924e-01 5.272924e-02 0 a
30 6.8 4.448581e-01 4.448581e-02 0 a
31 7.0 3.678794e-01 3.678794e-02 0 a
32 7.2 2.981973e-01 2.981973e-02 0 a
33 7.4 2.369278e-01 2.369278e-02 0 a
34 7.6 1.845195e-01 1.845195e-02 0 a
35 7.8 1.408584e-01 1.408584e-02 0 a
36 8.0 1.053992e-01 1.053992e-02 0 a
37 8.2 7.730474e-02 7.730474e-03 0 a
38 8.4 5.557621e-02 5.557621e-03 0 a
39 8.6 3.916390e-02 3.916390e-03 0 a
40 8.8 2.705185e-02 2.705185e-03 0 a
41 9.0 1.831564e-02 1.831564e-03 0 a
42 9.2 1.215518e-02 1.215518e-03 0 a
43 9.4 7.907054e-03 7.907054e-04 0 a
44 9.6 5.041760e-03 5.041760e-04 0 a
45 9.8 3.151112e-03 3.151112e-04 0 a
46 10.0 1.930454e-03 1.930454e-04 0 a
47 1.0 3.726653e-06 7.453306e-07 0 b
48 1.2 9.929504e-06 1.985901e-06 0 b
49 1.4 2.541935e-05 5.083869e-06 0 b
50 1.6 6.252150e-05 1.250430e-05 0 b
51 1.8 1.477484e-04 2.954967e-05 0 b
52 2.0 3.354626e-04 6.709253e-05 0 b
53 2.2 7.318024e-04 1.463605e-04 0 b
54 2.4 1.533811e-03 3.067621e-04 0 b
55 2.6 3.088715e-03 6.177431e-04 0 b
56 2.8 5.976023e-03 1.195205e-03 0 b
57 3.0 1.110900e-02 2.221799e-03 0 b
58 3.2 1.984109e-02 3.968219e-03 0 b
59 3.4 3.404745e-02 6.809491e-03 0 b
60 3.6 5.613476e-02 1.122695e-02 0 b
61 3.8 8.892162e-02 1.778432e-02 0 b
62 4.0 1.353353e-01 2.706706e-02 0 b
63 4.2 1.978987e-01 3.957974e-02 0 b
64 4.4 2.780373e-01 5.560746e-02 0 b
65 4.6 3.753111e-01 7.506222e-02 0 b
66 4.8 4.867523e-01 9.735045e-02 0 b
67 5.0 6.065307e-01 1.213061e-01 0 b
68 5.2 7.261490e-01 1.452298e-01 0 b
69 5.4 8.352702e-01 1.670540e-01 0 b
70 5.6 9.231163e-01 1.846233e-01 0 b
71 5.8 9.801987e-01 1.960397e-01 0 b
72 6.0 1.000000e+00 2.000000e-01 0 b
73 6.2 9.801987e-01 1.960397e-01 0 b
74 6.4 9.231163e-01 1.846233e-01 0 b
75 6.6 8.352702e-01 1.670540e-01 0 b
76 6.8 7.261490e-01 1.452298e-01 0 b
77 7.0 6.065307e-01 1.213061e-01 0 b
78 7.2 4.867523e-01 9.735045e-02 0 b
79 7.4 3.753111e-01 7.506222e-02 0 b
80 7.6 2.780373e-01 5.560746e-02 0 b
81 7.8 1.978987e-01 3.957974e-02 0 b
82 8.0 1.353353e-01 2.706706e-02 0 b
83 8.2 8.892162e-02 1.778432e-02 0 b
84 8.4 5.613476e-02 1.122695e-02 0 b
85 8.6 3.404745e-02 6.809491e-03 0 b
86 8.8 1.984109e-02 3.968219e-03 0 b
87 9.0 1.110900e-02 2.221799e-03 0 b
88 9.2 5.976023e-03 1.195205e-03 0 b
89 9.4 3.088715e-03 6.177431e-04 0 b
90 9.6 1.533811e-03 3.067621e-04 0 b
91 9.8 7.318024e-04 1.463605e-04 0 b
92 10.0 3.354626e-04 6.709253e-05 0 b
93 1.0 6.065307e-01 1.819592e-01 1 a
94 1.2 7.261490e-01 2.178447e-01 1 a
95 1.4 8.352702e-01 2.505811e-01 1 a
96 1.6 9.231163e-01 2.769349e-01 1 a
97 1.8 9.801987e-01 2.940596e-01 1 a
98 2.0 1.000000e+00 3.000000e-01 1 a
99 2.2 9.801987e-01 2.940596e-01 1 a
100 2.4 9.231163e-01 2.769349e-01 1 a
101 2.6 8.352702e-01 2.505811e-01 1 a
102 2.8 7.261490e-01 2.178447e-01 1 a
103 3.0 6.065307e-01 1.819592e-01 1 a
104 3.2 4.867523e-01 1.460257e-01 1 a
105 3.4 3.753111e-01 1.125933e-01 1 a
106 3.6 2.780373e-01 8.341119e-02 1 a
107 3.8 1.978987e-01 5.936961e-02 1 a
108 4.0 1.353353e-01 4.060058e-02 1 a
109 4.2 8.892162e-02 2.667649e-02 1 a
110 4.4 5.613476e-02 1.684043e-02 1 a
111 4.6 3.404745e-02 1.021424e-02 1 a
112 4.8 1.984109e-02 5.952328e-03 1 a
113 5.0 1.110900e-02 3.332699e-03 1 a
114 5.2 5.976023e-03 1.792807e-03 1 a
115 5.4 3.088715e-03 9.266146e-04 1 a
116 5.6 1.533811e-03 4.601432e-04 1 a
117 5.8 7.318024e-04 2.195407e-04 1 a
118 6.0 3.354626e-04 1.006388e-04 1 a
119 6.2 1.477484e-04 4.432451e-05 1 a
120 6.4 6.252150e-05 1.875645e-05 1 a
121 6.6 2.541935e-05 7.625804e-06 1 a
122 6.8 9.929504e-06 2.978851e-06 1 a
123 7.0 3.726653e-06 1.117996e-06 1 a
124 7.2 1.343812e-06 4.031437e-07 1 a
125 7.4 4.655716e-07 1.396715e-07 1 a
126 7.6 1.549753e-07 4.649259e-08 1 a
127 7.8 4.956405e-08 1.486922e-08 1 a
128 8.0 1.522998e-08 4.568994e-09 1 a
129 8.2 4.496349e-09 1.348905e-09 1 a
130 8.4 1.275408e-09 3.826223e-10 1 a
131 8.6 3.475891e-10 1.042767e-10 1 a
132 8.8 9.101471e-11 2.730441e-11 1 a
133 9.0 2.289735e-11 6.869205e-12 1 a
134 9.2 5.534610e-12 1.660383e-12 1 a
135 9.4 1.285337e-12 3.856012e-13 1 a
136 9.6 2.867975e-13 8.603925e-14 1 a
137 9.8 6.148396e-14 1.844519e-14 1 a
138 10.0 1.266417e-14 3.799250e-15 1 a
139 1.0 2.096114e-01 1.676891e-02 1 b
140 1.2 2.664683e-01 2.131746e-02 1 b
141 1.4 3.320399e-01 2.656320e-02 1 b
142 1.6 4.055545e-01 3.244436e-02 1 b
143 1.8 4.855369e-01 3.884295e-02 1 b
144 2.0 5.697828e-01 4.558263e-02 1 b
145 2.2 6.554063e-01 5.243250e-02 1 b
146 2.4 7.389685e-01 5.911748e-02 1 b
147 2.6 8.166865e-01 6.533492e-02 1 b
148 2.8 8.847059e-01 7.077647e-02 1 b
149 3.0 9.394131e-01 7.515305e-02 1 b
150 3.2 9.777512e-01 7.822010e-02 1 b
151 3.4 9.975031e-01 7.980025e-02 1 b
152 3.6 9.975031e-01 7.980025e-02 1 b
153 3.8 9.777512e-01 7.822010e-02 1 b
154 4.0 9.394131e-01 7.515305e-02 1 b
155 4.2 8.847059e-01 7.077647e-02 1 b
156 4.4 8.166865e-01 6.533492e-02 1 b
157 4.6 7.389685e-01 5.911748e-02 1 b
158 4.8 6.554063e-01 5.243250e-02 1 b
159 5.0 5.697828e-01 4.558263e-02 1 b
160 5.2 4.855369e-01 3.884295e-02 1 b
161 5.4 4.055545e-01 3.244436e-02 1 b
162 5.6 3.320399e-01 2.656320e-02 1 b
163 5.8 2.664683e-01 2.131746e-02 1 b
164 6.0 2.096114e-01 1.676891e-02 1 b
165 6.2 1.616212e-01 1.292970e-02 1 b
166 6.4 1.221507e-01 9.772054e-03 1 b
167 6.6 9.049144e-02 7.239315e-03 1 b
168 6.8 6.571027e-02 5.256822e-03 1 b
169 7.0 4.677062e-02 3.741650e-03 1 b
170 7.2 3.263076e-02 2.610460e-03 1 b
171 7.4 2.231491e-02 1.785193e-03 1 b
172 7.6 1.495813e-02 1.196651e-03 1 b
173 7.8 9.828195e-03 7.862556e-04 1 b
174 8.0 6.329715e-03 5.063772e-04 1 b
175 8.2 3.995846e-03 3.196677e-04 1 b
176 8.4 2.472563e-03 1.978050e-04 1 b
177 8.6 1.499685e-03 1.199748e-04 1 b
178 8.8 8.915937e-04 7.132750e-05 1 b
179 9.0 5.195747e-04 4.156597e-05 1 b
180 9.2 2.967858e-04 2.374286e-05 1 b
181 9.4 1.661699e-04 1.329359e-05 1 b
182 9.6 9.119596e-05 7.295677e-06 1 b
183 9.8 4.905836e-05 3.924669e-06 1 b
184 10.0 2.586810e-05 2.069448e-06 1 b
I have used facet_grid to generate this plot, but there is one thing that I am trying to figure out. So, the right panel is for z=0, and the left is for z=1. I want to move the line legend to inside the left panel (for Z=1) (top corner). I couldn't find the option for that.
And here is my code that I used in R to generate the plot:
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) +
+ geom_smooth(aes(ymin = yval-se, ymax = yval+se, linetype=cond, colour=cond, fill=cond), stat="identity", size=1.1) +
+ scale_colour_hue(l=25) +
+ ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2))
plot1 + facet_grid(~ zval, scales="free_y") + theme(strip.text.x = element_blank(),strip.background = element_rect(colour="white", fill="white"))
plot1 <- ggplot(data=df, aes(x=xval, y=yval, group=cond, colour=cond) ) +
geom_smooth(aes(ymin = yval-se, ymax = yval+se,
linetype=cond, colour=cond, fill=cond), stat="identity",
size=1.1) +
scale_colour_hue(l=25) +
ylim(-0.1,1.3) + scale_linetype_manual(values = c('a' = 1,'b' = 2))
The coordinates for legend.position are x- and y- offsets from the bottom-left of the plot, ranging from 0 - 1.
plot1 + facet_grid(~ zval, scales="free_y") +
theme(strip.text.x = element_blank(),
strip.background = element_rect(colour="white", fill="white"),
legend.position=c(.9,.75)
)
Tweak the legend.position values to suit your preference.

Resources