I have a dataset in which I am graphing means from 4 treatments over time, along with their standard errors, at 2 sites. The standard error bars are not being assigned properly to their respective means --they are going to both panels - can you please advise? See example:
d <- data.frame(site=rep(1:2,each=12),time=rep(1:3,8),trt=rep(rep(1:4,each=3),2))
d$mn <- rnorm(24,4,1)
d$se <- rnorm(24,2,1)
d$ul <- d$mn+d$se # create y value for standard error upper limit
my.panel <- function(x,y, ...){
panel.xyplot(x, y, ...)
panel.arrows(x, y, x, d$ul, length = 0.1,
angle = 90)
}
xyplot(mn ~ time|site,data=d,
group = trt,
type=c('p','l'),
panel = my.panel
)
Related
I am a basic-level learner of R. I am having a problem knitting out tables with a code my professor designed for the students. The code for table designs is set as below. I put this in my R markdown as below.
```{r, results="hide", message=FALSE, warning = FALSE, error = FALSE}
## my style latex summary of regression
jhp_report <- function(...){
output <- capture.output(stargazer(..., omit.stat=c("f", "ser")))
# The first three lines are the ones we want to remove...
output <- output[4:length(output)]
# cat out the results - this is essentially just what stargazer does too
cat(paste(output, collapse = "\n"), "\n")
}
```
After this, I tried printing this out with knitr.
```{r, message=FALSE, warning = FALSE, error = FALSE}
set.seed(1973)
N <- 100
x <- runif(N, 6, 20)
D <- rbinom(N, 1, .5)
t <- 1 + 0.5*x - .4*D + rnorm(N)
df.lm <- data.frame(y = y, x =x, D =D)
df.lm$D <- factor(df.lm$D, labels = c('Male', 'Female'))
##REGRESSION
reg.parallel <- lm(y ~ x + D, data = df.lm)
jhp_report(reg.parallel, title = "Result", label = "tab:D", dep.var.labels = "$y$")
```
As a result, instead of a table, it keeps on showing only the pure codes. I would like to know how I have to set up R markdown for it to print out the table instead of the codes. This is how the result looks like when I knit it.
I expected that there must be some setup options to print the table out. But I couldn't find the right one. Also, my assignment for class requires students to use this code. I did find other options like knitr::kable but I would like to use the given code for this assignment.
Thank you in advance!
I am using a list of files, and I am trying to create a data frame that contains: for each sample, the percentage of two particular "GT" types by the levels of another factor variable called "chr" (with 1 to 24 levels).
It would have to look like this:
The problem I keep getting is that the vector never gets updated for the ith sample, it only keeps the first vector created. And then I am not sure how to save that updated vector on my data frame (df).
vector_chr <- vector();
for (i in seq_along(list_files)) {
GT <- list_files[[i]][,9]
chr <- list_files[[i]][,3]
GT$chr <- chr$chr # creating one df with both GT and chr
for (j in unique(GT$chr)){
dat_list = split(GT, GT$chr) # split data frames by chr (1 to 24)
table <- table(dat_list[[j]][,1]) # take GT and make a table
sum <- sum(table[3:4]) # sum GTs 3 and 4
perc <- sum/nrow(GT)
vector_chr <- c(vector_chr,perc) # assign the 24 percentages to a vector
}
df <- data.frame(matrix(ncol = 25, nrow = length(files)))
x <- c("Sample", "chr1", "chr2", "chr3",
"chr4", "chr5", "chr6", "chr7", "chr8", "chr9", "chr10",
"chr11", "chr12","chr13", "chr14", "chr15", "chr16",
"chr17", "chr18", "chr19", "chr20", "chr21", "chr22",
"chrX", "chrXY")
colnames(df) <- x
df$Sample <- names(list_files)
df[i,2:25] <- vector_chr # assign the 24 percentages for EACH sample
}
I am using dygraphs for R and I opened the following issue on GitHub the other day, however, I have not yet received an answer. Therefore, I am hoping someone in here will be able to answer my question.
I want to know if it is possible to show all the values of the prediction interval in the legend, i.e. , lower, actual, upper, without having them as three separate plain dySeries? I like the look of the shading that the upper/lower bars bring, but I would also like to be able to hover over a point and see all the values for that particular point, not just the middle one. If such a function does not exists, is there an easy workaround, maybe with fillGraph = TRUE or something?
library(dygraphs)
hw <- HoltWinters(ldeaths)
p <- predict(hw, n.ahead = 72, prediction.interval = TRUE)
dygraph(p, main = "Predicted Lung Deaths (UK)") %>%
dySeries(c("lwr", "fit", "upr"), label = "Deaths")
The preceding code is the example from the web page, which is similar to my problem. I simply want to see the lwr and upr values in the legend when hovering.
So I found a workaround for anybody looking for something similar.
library(dygraphs)
hw <- HoltWinters(ldeaths)
p <- predict(hw, n.ahead = 72, prediction.interval = TRUE)
max <- p[,2]
min <- p[,3]
p <- ts.union(p, max, min)
dygraph(p, main = "Predicted Lung Deaths (UK)") %>%
dySeries(c("p.lwr", "p.fit", "p.upr"), label = "Deaths") %>%
dySeries("max", label = "Max", pointSize = 0.01, strokeWidth = 0.001) %>%
dySeries("min", label = "Max", pointSize = 0.01, strokeWidth = 0.001)
Obviously, this can be modified to suit your needs (e.g. color of the points etc.) The main idea in this method is simply to create two new columns containing the same information that is used in the bands, and then to make the lines to these too small to see.
I am trying to create a leaflet map as part of an R-Shiny project which displays circle markers at locations I previously geocoded from address information using the Google API. Putting the lon/lat values back into Google gives me the exact location of the address.
When I create the map from those lon/lat values in my shiny project using the code below, the marker positions are off in random directions by a few 100 meters (at maximum zoom level).
Based on google searches my guess is that it is something to do with markers changing position due to zoom level or with incompatible map projections.
output$mymap <- renderLeaflet({
alldata_sel = alldata
if(input$dachverbandCheck != T){alldata_sel = filter(alldata_sel, Dachverband==input$dachverband)}
if(input$landkreisCheck != T){alldata_sel = filter(alldata_sel, Landkreis==input$landkreis)}
if(input$leistungstypCheck != T){alldata_sel = filter(alldata_sel,
LeistungstypBezeichnung==input$leistungstyp)}
if(input$traegerCheck != T){alldata_sel = filter(alldata_sel, Traegername==input$traeger)}
#initialize map and setView
leaflet(options = leafletOptions(minZoom = 5, maxZoom = 18)) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addCircleMarkers(
data = alldata_sel,
lng=~longitude, # Longitude coordinates
lat=~latitude, # Latitude coordinates
radius=~KapRadius,
stroke=FALSE, # Circle stroke
fillOpacity=0.5, # Circle Fill Opacity
color = rgb(alldata_sel$colour_r, alldata_sel$colour_g, alldata_sel$colour_b,
maxColorValue = 255)
)
})
I am trying to show navigation in R plot. The current status in time one or (t1) is set as val$currentstatus and next status in (t2) wants to be shown in the graph based on the action that the user choose from the checkbook. then I want to draw a line to show this path. The code that I wrote is as following
output$navigation<-renderPlot({
#initial state of X and Y
if(is.element("Within", vals$currentstatus))
x <- 1
y <- 2
if(is.element("Out", vals$currentstatus)) {
x <- 1
y <- 1
}
action<-c(input$action1,input$action2)
x<-1:4
y<-1:2
rewards<-matrix(rep(0,8),nrow=2)
rewards[1,4]<- -1
rewards[2,4]<- 1
values<-rewards#initial Values
states<-expand.grid(x=x,y=y)
#Transition probability
transition <- list( input$action1= c("within" = 0.8, "out" = .2),
input$action2= c("within" = 0.3, "out" = .7))
# The value of an action (e.g. move toward benchmark )
action.values <- list(input$action1 = c("x" = 1, "y" = 0),
input$action1 = c("x" = 1, "y" = 0))
# act() function serves to move the agent to go to new position
act <- function(action, state) {
action.value <- action.values[[action]]
new.state <- state
#
if(state["x"] == 4 && state["y"] == 1 || (state["x"] == 4 && state["y"] == 2))
return(state)
#
new.x = state["x"] + action.value["x"]
new.y=if(transition["within">"out"]){state["y"==2]}
if(transition["within"<"out"]){state["y"==1]}
}
plot(x, y, xaxt='n',yaxt='n',cex=10,pch=19,
col=ifelse(y==1,"red","green"),ylab="status",xlab="period",
xlim=c(0,4), ylim=c(0,3))
axis(1,at=1:4,labels=c("t1","t2","t3","t4"))
axis(2,at=1:2,labels=c("out bench","within bench"))
if next position is within bench it should be green and connect to the previous state and if it is out of bench should be red and connect to previous state. Also I want to see the name of chosen action on the connection line between two states.Moreover I want to know how can I update the new position and use it for calculating next state in next period (t3) and so force.Similar to the following graph: