I am trying to run rWBclimate package in RStudio. I copied the below code from ROpenSci and pasted in RStudio. But I get error saying 'Don't know how to automatically pick scale for object of type list. Defaulting to continuous
Error: geom_point requires the following missing aesthetics: y
gbr.dat.t <- get_ensemble_temp("GBR", "annualavg", 1900, 2100)
## Loading required package: rjson
### Subset to just the median percentile
gbr.dat.t <- subset(gbr.dat.t, gbr.dat.t$percentile == 50)
## Plot and note the past is the same for each scenario
ggplot(gbr.dat.t,aes(x=fromYear,y=data,group=scenario,colour=scenario))
+ geom_point() +
geom_path() +
theme_bw() +
xlab("Year") +
ylab("Annual Average Temperature in 20 year increments")
I also tried to use geom_point(stat="identity") in the following way but didn't work:
ggplot(gbr.dat.t,aes(x=fromYear,y=data,group=scenario,colour=scenario))
+ geom_point(stat="identity") +
geom_path() +
theme_bw() +
xlab("Year") +
ylab("Annual Average Temperature in 20 year increments")
I still get the same message "Don't know how to automatically pick scale for object of type list. Defaulting to continuous
Error: geom_point requires the following missing aesthetics: y"
Also, the result from str(gbr.dat.t) is given below:
> str(gbr.dat.t)
'data.frame': 12 obs. of 6 variables:
$ scenario : chr "past" "past" "past" "past" ...
$ fromYear : int 1920 1940 1960 1980 2020 2020 2040 2040 2060 2060 ...
$ toYear : int 1939 1959 1979 1999 2039 2039 2059 2059 2079 2079 ...
$ data :List of 12
..$ : num 9.01
..$ : num 9.16
..$ : num 9.05
..$ : num 9.36
..$ : num 10
..$ : num 9.47
..$ : num 9.92
..$ : num 10.7
..$ : num 10.3
..$ : num 11.4
..$ : num 12.1
..$ : num 10.4
$ percentile: int 50 50 50 50 50 50 50 50 50 50 ...
$ locator : chr "GBR" "GBR" "GBR" "GBR" ...
Looking for your helpful answers.
Hope this helps. All I did was convert the gbr.dat.t$data to a numeric vector
library('rWBclimate')
library("ggplot2")
gbr.dat.t <- get_ensemble_temp("GBR", "annualavg", 1900, 2100)
## Loading required package: rjson
### Subset to just the median percentile
gbr.dat.t <- subset(gbr.dat.t, gbr.dat.t$percentile == 50)
#This is the line you were missing
gbr.dat.t$data <- unlist(gbr.dat.t$data)
## Plot and note the past is the same for each scenario
ggplot(gbr.dat.t,aes(x=fromYear,y=data,group=scenario,colour=scenario)) + geom_point() +
geom_path() +
theme_bw() +
xlab("Year") +
ylab("Annual Average Temperature in 20 year increments")
Related
I'm having problems with my ggplot2 drawing, I don't know why, I've restarted Rstudio and its theme system can't be restored to the original, which is the default theme
library(tidyverse)
chic <- read_csv("./chicago-nmmaps-custom.csv")
ggplot(chic, aes(x = date, y = temp)) +
geom_point()
Here's the code I ran
This is what I got when I ran it
Normal should look like this, as shown below
You could use theme_set to replace older themes like this:
library(ggplot2)
p <- ggplot(mtcars, aes(mpg, wt)) +
geom_point()
p
old <- theme_set(theme_bw())
p
theme_set(old)
p
Created on 2022-10-08 with reprex v2.0.2
The problem is that column date is not a date object, it's a column of class "character". Coerce to class "Date" and the default grey theme is used.
The output of str shows the data set columns' classes and date is displayed as chr, meaning, a column of class "character". R has real dates and times classes and this column must become one. Everything afterwards will be easier, including ggplot2 code. ggplot2's layers scale_*_date and scale_*_datetime even have special date and date/time breaks and labels arguments, respectively.
str(chic)
#> 'data.frame': 5114 obs. of 9 variables:
#> $ city : chr "chic" "chic" "chic" "chic" ...
#> $ date : chr "1987-01-01" "1987-01-02" "1987-01-03" "1987-01-04" ...
#> $ death : int 130 150 101 135 126 130 129 109 125 153 ...
#> $ temp : num 31.5 33 33 29 32 40 34.5 29 26.5 32.5 ...
#> $ dewpoint: num 31.5 29.9 27.4 28.6 28.9 ...
#> $ pm10 : num 27.8 NA 33.7 40.8 NA ...
#> $ o3 : num 4.03 4.58 3.4 3.94 4.4 ...
#> $ time : int 1 2 3 4 5 6 7 8 9 10 ...
#> $ season : chr "winter" "winter" "winter" "winter" ...
library(ggplot2)
chic |>
dplyr::mutate(date = as.Date(date)) |>
ggplot(aes(date, temp)) +
geom_point() +
scale_x_date(date_breaks = "1 year", date_labels = "%Y")
Created on 2022-10-08 with reprex v2.0.2
I have two data set that looks like:
> str(Maj.plot)
'data.frame': 70 obs. of 13 variables:
$ Sample : chr "Kx V17" "Mu V17" "Ob V17" "Vä V17" ...
$ Mill : chr "Karlsborg" "Munksund" "Obbola" "Väja" ...
$ Halfyear: chr "S17" "S17" "S17" "S17" ...
$ Al : num 0.355 0.593 0.804 0.318 0.847 ...
$ Ca : num 17.6 14.1 15.1 24 14.1 ...
$ Fe : num 0.315 0.455 0.413 0.224 0.776 ...
$ K : num 0.639 0.473 0.324 0.955 0.216 ...
$ Mg : num 5.36 9.51 10.36 2.84 12.25 ...
$ Mn : num 1.46 3.11 3.2 1.49 4.25 ...
$ Na : num 7.08 5.31 2.23 8.45 2.79 ...
$ P : num 0.096 0.144 0.144 0.6023 0.0829 ...
$ Si : num 0.767 0.467 1 1.271 3.613 ...
$ Ti : num NA NA NA NA 0.018 ...
> str(DL.major)
'data.frame': 1 obs. of 10 variables:
$ Al: num NA
$ Ca: num NA
$ Fe: num 0.00699
$ K : num NA
$ Mg: num NA
$ Mn: num 0.00774
$ Na: num NA
$ P : num 0.00436
$ Si: num NA
$ Ti: num 0.00599
I have plotted this data with both facet_wrap and gather:
ggplot(gather(Maj.plot, key=Element.major, value="value", -"Sample", -"Mill", -"Halfyear"),
aes(y=value, x=Mill, color=Mill, shape=Halfyear)) +
geom_point() +
facet_wrap(~ Element.major, scales = 'free', ncol=3)
Added to this plot is also stuff like theme, ggtitel() and guides etc not displayed here (not sure if it impacts the answer or not).
What I would like is add different lines to some of the facets, like a geom_hline() from the data set DL.
So as an example, for the facet with the name Fe I would like an yintercept-line at y= 0.00699 (they cant be seen for this data set but I have other bigger sets where the the scale for y is small enough that the line will be visible). But no line for the Al facet.
I tried:
ggplot(gather(Maj.plot, key=Element.major, value="value", -"Sample", -"Mill", -"Halfyear"),
aes(y=value, x=Mill, color=Mill, shape=Halfyear)) +
geom_point() +
facet_wrap(~ Element.major, scales = 'free', ncol=3) +
geom_hline(data= DL.major)
But geom_hline requires one to set yintercept and I do not know what to specify there so that did not work. I also tried:
DL.major.1 <-t(DL.major)
colnames(DL.major.1) <- "DL"
ggplot(gather(Maj.plot, key=Element.major, value="value", -"Sample", -"Mill", -"Halfyear"),
aes(y=value, x=Mill, color=Mill, shape=Halfyear)) +
geom_point() +
facet_wrap(~ Element.major, scales = 'free', ncol=3) +
geom_hline(data= DL.major.1, aes(yintercept = DL))
Using this example: How to add different lines for facets But that got me all lines in all facets and that is not what I wanted.
How can it be done (and can it be done)?
Perhaps something like this?
(BTW it is good practice to make your question reproducible, either by using a built-in data set or by including a sample of data structured like yours by either using dput(YOUR_DATA) or providing code that generates it.)
mtcars %>%
ggplot(aes(wt, mpg)) +
geom_point() +
#important, this should have the faceted variable
geom_hline(data = tibble(gear = 3, y = 30), aes(yintercept = y)) +
facet_wrap(~gear)
In addition to Jon Springs solution above for sending one/a few values to geom_line there is a solution if one have a lot of lines to add (like if one displays several elemental analysis's and wants to add different detection levels to some elements):
ggplot(gather(df, key=KeyVector, value="value", ),
aes(y=value, x=Xvalues, color=ColValues, shape=ShapeValues)) +
geom_point() +
#geom_histogram() +
facet_wrap(~ KeyVector, scales = 'free') +
geom_hline(data = tibble(KeyVector, y = unlist(HlineValues)), aes(yintercept=y) )
> dput(KeyVector)
c("Al", "Ca", "Fe", "K", "Mg", "Mn", "Na", "P", "Si", "Ti")
> dput(HlineValues)
structure(list(Al = NA_real_, Ca = NA_real_, Fe = 0.00699430761427042,
K = NA_real_, Mg = NA_real_, Mn = 0.00774461846427111, Na = NA_real_,
P = 0.00436426817378561, Si = NA_real_, Ti = 0.00599348901270895), class = "data.frame", row.names = "y")
To be clear for others with similar knowledge as me the variable HlineValues is a data frame with one row (that contains the data to become the hline) that must have the same length as KeyVector, colnames(HlineValues) are the same as what is in KeyVector.
I have two data sets MASS and MASS2 to create a map in R. I got the first one with the help of library(ggmap).
counties<-map_data('county')
MASS<-map_data('county', 'massachusetts')
str(MASS)
data.frame': 744 obs. of 6 variables:
$ long : num -70.7 -70.5 -70.5 -70.5 -70.5 ...
$ lat : num 41.7 41.8 41.8 41.8 41.8 ...
$ group : num 1 1 1 1 1 1 1 1 1 1 ...
$ state : chr "massachusetts" "massachusetts" "massachusetts" ...
$ county_name: chr "barnstable" "barnstable" "barnstable" "barnstable" ...
The second consists of 14 points one per each county and has a teacher's quantity data per that county.
str(MASS2)
'data.frame': 14 obs. of 6 variables:
$ state : chr "massachusetts" "massachusetts" "massachusetts" ...
$ county_name : chr "barnstable" "berkshire" "bristol" "dukes" ...
$ long : num -70.7 -73.5 -71.2 -70.5 -71 ...
$ lat : num 41.7 42 41.7 41.4 42.4 ...
$ group : num 1 2 3 4 5 6 7 8 9 10 ...
$ teacher_count: int 62 40 47 ...
I need to create a map where each teacher_count point will be represented by a circle in accordance with teacher's amount. So far I'm getting just one size circles.
My code is next:
ggplot(MASS, aes(long,lat, group = group)) +
geom_polygon(aes(fill = county_name),colour = "black") +
geom_point(data = MASS2, aes(x = long, y = lat), color = "red", size = 5)+
theme(legend.position="none") +
coord_quickmap()
This is the map I get
I found one solution online which offers to represent the size in geom_point as
+geom_point(......, size = MASS2$teacher_count*circle_scale_amt)+
scale_size_continuous(range=range(MA$teacher_count))
but R can't find circle_scale_amt.
I am a new to R and trying to learn. Will appreciate ideas for any other ways to represent the teachers by their quantity! Thank you!
This works for me after setting a value for circle_scale_amt to rescale the size of the points otherwise they would be too big.
library(ggmap)
counties <- map_data('county')
MASS <- map_data('county', 'massachusetts')
circle_scale_amt <- 0.05
ggplot(MASS, aes(long,lat, group = group)) +
geom_polygon(aes(fill = subregion),colour = "black") +
geom_point(data = MASS2, aes(x = long, y = lat),
size = MASS2$teacher_count * circle_scale_amt,
color = "red", alpha = 0.6)+
scale_size_continuous(range = range(MASS2$teacher_count)) +
theme(legend.position="none") +
coord_quickmap()
Created on 2018-03-16 by the reprex package (v0.2.0).
I would like to cross-classify and plot bal using qplot facets:
> str(bal)
'data.frame': 2096 obs. of 6 variables:
$ fips : chr "24510" "24510" "24510" "24510" ...
$ SCC : chr "10100601" "10200601" "10200602" "30100699" ...
$ Pollutant: chr "PM25-PRI" "PM25-PRI" "PM25-PRI" "PM25-PRI" ...
$ Emissions: num 6.53 78.88 0.92 10.38 10.86 ...
$ type : chr "POINT" "POINT" "POINT" "POINT" ...
$ year : int 1999 1999 1999 1999 1999 1999 1999 1999 1999 1999 ...
I'm interested in the two classifiers year and type:
> levels(factor(bal$year))
[1] "1999" "2002" "2005" "2008"
> levels(factor(bal$type))
[1] "NON-ROAD" "NONPOINT" "ON-ROAD" "POINT"
I get it so far, that I can plot the distribution of Emissions cross-classified by year and type:
What I'm unable to do is to plot the sum of the distributions of each year, which I however am able to compute:
> tapply(bal$Emissions, list(bal$year, bal$type), sum)
NON-ROAD NONPOINT ON-ROAD POINT
1999 522.94000 2107.625 346.82000 296.7950
2002 240.84692 1509.500 134.30882 569.2600
2005 248.93369 1509.500 130.43038 1202.4900
2008 55.82356 1373.207 88.27546 344.9752
My guess was something along the lines of
> qplot(bal$year, tapply(bal$Emissions, list(bal$year, bal$type), sum),
data=bal, facets= . ~ type)
Error: Aesthetics must either be length one, or the same length as the
dataProblems:tapply(bal$Emissions, list(bal$year, bal$type), sum)
but I dont get what R is telling me there.
How can I plot this matrix using qplot?
You dan do that using ggplot with either
qplot(year, Emissions, data=bal,
stat="summary", fun.y="sum",
facets= .~type
)
or
ggplot(bal) +
aes(year, Emissions) +
stat_summary(fun.y="sum",geom="point") +
facet_grid(.~type)
Both should give you the following plot which seems to match up well to your summary data.
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
My lack of understanding of R is causing me to grind to a halt in my work and seek your help. I'm looking to build a neural network from some time series data and then build a prediction using separate data and the model returned by the trained neural network.
I created an xts containing the dependent variable nxtCl (a one-day forward closing stock price) and the independent variables (a set of corresponding prices and technical indicators).
I split the xts in two, one set being training data and the other set for testing/prediction, these are miData.train and miData.test respectively. Subsequently I altered these two xts to be scaled data frames.
miData.train <- scale(as.data.frame(miData.train))
miDate.test <- scale(as.data.frame(miData.test))
Using the package nnet I am able to build a neural network from the training data:
nn <- nnet(nxtCl ~ .,data=miData.train,linout=T,size=10,decay=0.001,maxit=10000)
The str() output for this returned formula object is:
> str(nn)
List of 18
$ n : num [1:3] 11 10 1
$ nunits : int 23
$ nconn : num [1:24] 0 0 0 0 0 0 0 0 0 0 ...
$ conn : num [1:131] 0 1 2 3 4 5 6 7 8 9 ...
$ nsunits : num 22
$ decay : num 0.001
$ entropy : logi FALSE
$ softmax : logi FALSE
$ censored : logi FALSE
$ value : num 4.64
$ wts : num [1:131] 2.73 -1.64 1.1 2.41 1.36 ...
$ convergence : int 0
$ fitted.values: num [1:901, 1] -0.465 -0.501 -0.46 -0.431 -0.485 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:901] "2005-07-15" "2005-07-18" "2005-07-19" "2005-07-20" ...
.. ..$ : NULL
$ residuals : num [1:901, 1] -0.0265 0.0487 0.0326 -0.0384 0.0632 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : chr [1:901] "2005-07-15" "2005-07-18" "2005-07-19" "2005-07-20" ...
.. ..$ : NULL
$ call : language nnet.formula(formula = nxtCl ~ ., data = miData.train, inout = T, size = 10, decay = 0.001, maxit = 10000)
$ terms : language nxtCl ~ Op + Hi + Lo + Cl + vul + smaten + smafif + smath + vol + rsi + dvi
$ coefnames : chr [1:11] "Op" "Hi" "Lo" "Cl" ...
$ xlevels : Named list()
- attr(*, "class")= chr [1:2] "nnet.formula" "nnet"
I then try to run the prediction function using this model nn and the data I kept separate miData.test using the following function:
preds <- predict(object=nn, miData.test)
and I get the following error:
Error in terms.default(object, data = data) :
no terms component nor attribute
Running terms.default on miData.test I see that my data frame does not have any attributes:
terms.default(miData.test)
Error in terms.default(miData.test) : no terms component nor attribute
but is this why the prediction will not run?
miData.test has names that match the terms of nn:
> nn$terms
nxtCl ~ Op + Hi + Lo + Cl + vul + smaten + smafif + smath + vol +
rsi + dvi
> names(miData.test)[1] "Op" "Hi" "Lo" "Cl" "vul" "smaten" "smafif" "smath" "vol" "rsi" "dvi" "nxtCl"
And, in terms of structure, the data is exactly the same as that which was used to build nn in the first place. I tried adding my own named attributes to miData.test, matching the terms of nn but that did not work. The str() of miData.test returns:
> str(miData.test)
'data.frame': 400 obs. of 12 variables:
$ Op : num 82.2 83.5 80.2 79.8 79.8 ...
$ Hi : num 83.8 84.2 83 79.9 80.2 ...
$ Lo : num 81 82.7 79.2 78.3 78 ...
$ Cl : num 83.7 82.8 79.2 79 78.2 ...
$ vul : num 4.69e+08 2.94e+08 4.79e+08 3.63e+08 3.17e+08 ...
$ smaten: num 84.1 84.1 83.8 83.3 82.8 ...
$ smafif: num 86.9 86.8 86.7 86.6 86.4 ...
$ smath : num 111 111 111 110 110 ...
$ vol : num 0.335 0.341 0.401 0.402 0.382 ...
$ rsi : num 45.7 43.6 36.6 36.3 34.7 ...
$ dvi : num 0.00968 0.00306 -0.01575 -0.01189 -0.00623 ...
$ nxtCl : num 82.8 79.2 79 78.2 77.4 ...
Any help or insight in getting predict() to work in this instance would be greatly appreciated. Thanks.
Here's some reproducible code. In putting this together, I have 'removed' the error. Unfortunately, although it now works, I am none the wiser as to what was causing the problem before:
require(quantstrat)
require(PerformanceAnalytics)
require(nnet)
initDate <- "2004-09-30"
endDate <- "2010-09-30"
symbols <- c("SPY")
getSymbols(symbols, from=initDate, to=endDate, index.class=c("POSIXt","POSIXct"))
rsi <- RSI(Cl(SPY))
smaTen <- SMA(Cl(SPY))
smaFif <- SMA(Cl(SPY),n=50)
nxtCl <- lag(Cl(SPY),-1)
tmp <- SPY[,-5]
tmp <- tmp[,-5]
miData <- merge(tmp,rsi,smaTen,smaFif,nxtCl)
names(miData) <- c("Op","Hi","Lo","Cl","rsi","smaTen","smaFif","nxtCl")
miData <- miData[50:1512]
scaled.miData <- scale(miData)
miData.train <- as.data.frame(scaled.miData[1:1000])
miData.test <- as.data.frame(scaled.miData[1001:1463])
nn <- nnet(nxtCl ~ .,data=miData.train,linout=T,size=10,decay=0.001,maxit=10000)
preds <- predict(object=nn, miData.test)