I have a simple example using tooltips with rCharts that doesn't seem to work:
set.seed(1)
test <- data.frame(x = rnorm(100), y = rnorm(100))
rPlot(y ~ x, data = test,
type = 'point',
tooltip = "function(item){return item.x + '\n' + item.name + '\n' + item.y}")
An empty page comes up. The plot is there if I remove the tooltip option. I'm using rCharts_0.4.1, R Under development on x86_64-apple-darwin10.8.0 (64-bit) and version 31.0.1650.63 of Chrome.
Bonus question! Can tooltips contain variables in the data set but not used in x, y, etc? I have a large data set and I'd like to annotate the data points with an ID variable that has a unique value per row.
Thanks,
Max
Rcharts 0.4.2
I can't point out where I've seen this before, but the best I can offer is that the current instruction is to wrap your js function like so:
"#!function(item) { return item.x }!#"
set.seed(1)
test <- data.frame(x = rnorm(100), y = rnorm(100), id = 1:100)
p <- rPlot(y ~ x, data = test,
type = 'point',
tooltip = "#!function(item){ return 'x: ' + item.x +
' y: ' + item.y + ' id: ' + item.id }!#")
I could never get the tips on different lines. You can see the problem by typing p$show('inline') and including line breaks where you want them when creating the tooltip. Mustache is converting the linebreaks, which means they disappear in the resulting JS function and cause the tooltip function to span several lines - causing an error. I've tried to escape the newline character and append each string containing a newline character with .replace('\\n', '\n'), but that obviously results in the same problem.
For the time being, your best bet is to make the plot as shown here, type p$save('filepath.html') and manually add the line breaks. Otherwise, the question is how to pass a literal newline character to mustache.
Your code works/worked using rCharts 0.3.51 (Chrome 31.0.1650.63 m, Win7 x64):
set.seed(1)
require(rCharts)
test <- data.frame(x = rnorm(100), y = rnorm(100),
name=replicate(100, paste(sample(letters, 5, rep=T), collapse="")))
rPlot(y ~ x, data = test,
type = 'point',
tooltip = "function(item){return item.x + '\n' + item.name + '\n' + item.y}")
You should be able to reference all columns in test - as I did with name.
Related
I want to prepare the plot's data and aes layers. But this code doesn't run
p <- ggplot(data = mov2, aes(x = Genre, y = Gross % US))
when aes layers is taken off, it's working
p <- ggplot(data = mov2)
p <- ggplot(data = mov2, aes(x = Genre, y = Gross % US)) # this code got error
v <- ggplot(data = movies, aes(x = Genre, y = CriticRating)) #this code is working
Error: unexpected input in "p<-ggplot(data=mov2, aes(x=Genre,y=Gross % US))"
Most R code will get confused with columns that have spaces or weird symbols like %. You need to surround those with backticks to R knows that's supposed to be a column name. Try
p <- ggplot(data=mov2, aes(x=Genre,y=`Gross % US`))
I'm attempting to plot an rpart tree where I'd like to change some of the split labels to their greek/math equivalent. For instance, I have a column named mu -- I'd like this to show up as the greek letter $\mu$.
Unfortunately, when I replace one of the labels, it results in the error "Error in strsplit(labs, "\n\n") : non-character argument". As I'm not using strsplit, this error must be coming from rpart.plot call where it is assuming the labels are all plain text. This is my code:
split.fun <- function(x, labs, digits, varlen, faclen)
{
for(i in 1:length(labs)) {
if(substring(labs[i],0,2)=="mu"){
#labs[i] <- bquote(mu ~ .(substring(labs[i],3)))
labs[i] <- expression(paste0(mu,substring(labs[i],3)))
}
print(labs[i])
}
labs
}
data$dv <- factor(data$dv, labels = c("No", "Yes"))
fit <- rpart(dv ~ n + alpha + dev + mu, method="class", data=data)
rpart.plot(fit, yesno=2, box.palette = 0, extra=100, under = TRUE, split.fun = split.fun)
Neither the "expression" approach or "bquote" approach work. However, the split.fun function works fine as long as I just replace substrings with other strings (not expressions).
In trying to figure out what's going on, I've also been printing out the resulting labels. This is what I get:
[1] "root"
[1] "dev >= 0.075"
expression(paste0(mu, substring(labs[i], 3)))
expression(paste0(mu, substring(labs[i], 3)))
expression("alpha < 0.025")
expression("alpha >= 0.025")
expression("dev < 0.075")
expression("alpha < 0.025")
expression("dev >= 0.025")
expression(paste0(mu, substring(labs[i], 3)))
expression(paste0(mu, substring(labs[i], 3)))
expression("dev < 0.025")
expression("alpha >= 0.025")
From this, it seems that once I replace one label with an expression, all other labels are replaced with an expression.
Is there another approach to placing greek letters on the rpart.plot? Or is rpart.plot (or prp in general), simply not capable of including math expressions?
A combination fo #G5W's suggestion and fonts work. For those trying to do this, add the following to the top of the file:
library(extrafont)
loadfonts()
Then in adjust the rpart.plot call to use "Arial Unicode MS". This font seems to always correctly display math unicode characters (including combining characters).
rpart.plot(fit, yesno=2, box.palette = 0, extra=100, under = TRUE, split.fun = split.fun, split.font=1, split.family="Arial Unicode MS", family="Arial Unicode MS")
I have some data, and I want to use some variables from stat_count() to label a bar plot.
This is what I want to do:
library(ggplot2)
library(scales)
percent_and_count <- function(pct, cnt){
paste0(percent(pct), ' (', cnt, ')')
}
ggplot(aes(x=Type)) +
stat_count(aes(y=(..prop))) +
geom_text(aes(y=(..prop..), label=percent_and_count(..prop.., ..count))),
stat='count')
However, I get this error, since it can't find the function in what I assume is either some base packages or the data frame:
Error in eval(expr, envir, enclos) : could not find function "percent_and_count"
I get this error if I do percent(..prop..) as well, although it is fine with scales::percent(..prop..). I did not load my function from a package.
If all else fails, I can do
geom_text(aes(y=(..prop..), label=utils::getAnywhere('percent_and_count')$objs[[1]]((..prop..),(..count..))))
But this seems needlessly roundabout for what should be a stupidly simple task.
You can use bquote and aes_:
# Sample data
set.seed(2017);
df <- data.frame(
Type = sample(6, 100, replace = T)
);
library(ggplot2);
library(scales);
# Your custom function
percent_and_count <- function(pct, cnt){
paste0(percent(pct), ' (', cnt, ')')
}
ggplot(df, aes(x = Type)) +
stat_count(aes(y = ..prop..)) +
geom_text(
stat = "count",
aes_(
y = ~(..prop..),
label = bquote(.(percent_and_count)((..prop..), (..count..)))))
Explanation: bquote(.(percent_and_count)(...)) ensures that percent_and_count is found (as terms .(...) are evaluated in the parent environment). We then use aes_ to ensure that quoted expressions (either with ~ or bquote) are properly evaluated.
Still not pretty, but probably more straighforward than using utils::getAnywhere.
I want to include math symbols in the panel titles for this stratigraphic plot:
library(analogue)
data(V12.122)
Depths <- as.numeric(rownames(V12.122))
names(V12.122)
(plt <- Stratiplot(Depths ~ O.univ + G.ruber + G.tenel + G.pacR,
data = V12.122,
type = c("h","l","g"),
zones = 400))
plt
For example, I want to have this text in place of "O.univ" etc.:
I used this code to make that text:
plot(1, type="n", axes=FALSE, ann=FALSE)
title(line = -1, main = expression(phantom()^14*C~years~BP))
title(line = -3, main = expression(delta^18*O))
title(line = -5, main = expression(paste("TP ", mu,"g l"^-1)))
title(line = -10, main = expression("very long title \n with \n line breaks"))
But if I try to update the colnames of the data frame passed to Stratiplot, the code is not parsed, and we do not get the correct text formatting:
V12.122 <- V12.122[, 1:4]
names(V12.122)[1] <- expression(phantom()^14*C~years~BP)
names(V12.122)[2] <- expression(delta^18*O)
names(V12.122)[3] <- expression(paste("TP ", mu,"g l"^-1))
(plt <- Stratiplot(Depths ~ .,
data = V12.122,
type = c("h","l","g"),
zones = 400))
plt
How can I get Stratiplot to parse the expressions in the colnames and format them correctly in the plot?
I've tried looking through str(plt) to see where the panel titles are stored, but no success:
text <- expression(phantom()^14*C~years~BP)
plt$condlevels$ind[1] <- text
names(plt$packet.sizes)[1] <- text
names(plt$par.settings$layout.widths$panel)[1] <- text
You can't actually do this in the current release of analogue; the function is doing too much messing around with data for the expressions to remain unevaluated prior to plotting. I could probably figure this out to allow expressions as the names of the data argument object, but it is easier to just allow users to pass a vector of labels that they want for the variables.
This is now implemented in the development version of the package on github, and I'll push this to CRAN early next week.
This change implements a new argument labelValues which takes a vector of labels for use in labelling the top axis. This can be a vector of expressions.
Here is an illustration of the usage:
library("analogue")
set.seed(1)
df <- setNames(data.frame(matrix(rnorm(200 * 3), ncol = 3)),
c("d13C", "d15N", "d18O"))
df <- transform(df, Age = 1:200)
exprs <- expression(delta^{13}*C, # label for 1st variable
delta^{15}*N, # label for 2nd variable
delta^{18}*O) # label for 3rd variable
Stratiplot(Age ~ ., data = df, labelValues = exprs, varTypes = "absolute", type = "h")
which produces
Note that this is just a first pass; I'm pretty sure I haven't accounted for any reordering that goes on with sort and svar etc. if they are used.
Never used lattice plots, but I thought a chance to learn something should be worth while. Took too long to figure out.
text <- "c( expression(phantom()^14*C~years~BP),expression(delta^18*O))"
strip = strip.custom(factor.levels=eval(parse(text=text)))
plt <- Stratiplot(Depths ~ .,
data = V12.122[, 1:4],
type = c("h","l","g"),
zones = 400,
strip = strip)
Hope this gets you started.
hoping someone can offer some guidance here.
I'm creating a multivariate simulation using the simDesign package, I am varying the number of factors as well as items that load on each factor. I would like to write a command that identifies the number of factors present in factornumbers and assigns the appropriate items to them (no cross loading). I will be testing all combinations of the conditions below and more, and I would like to have a model command that acknowledge the iterations of differing models, so I don't have to write multiple model statements.
factornumbers<-c(1,2,3,5)
itemsperfactor<-c(5,10,30)
What lavaan and mirt are looking for is below:
mirtmodel<-mirt.model('
F1=1-15
F2=16-30
MEAN=F1,F2
COV=F1*F2')
lavmodel <- ' F1=~ Item_1 + Item_2 + Item_3 + Item_4 + Item_5 + Item_6 + Item_7 + Item_8 + Item_9 + Item_10 + Item_11 + Item_12 + Item_13 + Item_14 + Item_15
F2=~ Item_16 + Item_17 + Item_18 + Item_19 + Item_20 + Item_21 + Item_22 + Item_23 + Item_24 + Item_25 + Item_26 + Item_27 + Item_28 + Item_29 + Item_30'
The simDesign package offers this example, I would like to expand on it but I'm not sure I have the know-how:
lavmodel<-paste0('F=~ ', paste0(colnames(dat)[1L], ' + '),
paste0(colnames(dat)[-1L], collapse = ' + '))
What I would like is a single mirt and lavaan command that finds the number of factors specified in the factornumbers command and assigns the correct items specified in the data as well as itemsperfactor.
EDIT:
I would like the model identification to pick up on which factor & item structure is in use for that condition and fill in the model identification with the correct information.
For Example:
mirtmodel<-mirt.model('
F1=1-1
F2=6-10
F3=11-15
F4=16-20
F5=21-25
MEAN=F1,F2,F3,F4,F5
COV=F1*F2*F3*F4*F5')
Or
mirtmodel<-mirt.model('
F1=1-30
F2=31-60
MEAN=F1,F2
COV=F1*F2')
And also the corresponding lavaan models.
The idea here is to paste different strings together so that the condition input (row of the respective Design object) is all that is required to construct a suitable model specification string. Generating syntax for simulations is arguably the most annoying part of simulations, but at least in R there are a good number of helpful string operations (plus, packages like stringr).
Here's my interpretation of what you are currently looking for using base R functions.
library(SimDesign)
library(mirt)
Design <- createDesign(factornumbers = c(1,2,3,5),
itemsperfactor = c(5,10,30))
gen_syntax_mirt <- function(condition){
fn <- with(condition, factornumbers)
ipf <- with(condition, itemsperfactor)
nitems <- fn * ipf
maxloads <- sort(seq(nitems, ipf, length.out = fn))
minloads <- c(1, maxloads[-length(maxloads)] + 1)
fnames <- paste0('F', 1:fn)
df <- cbind(fnames, ' = ', minloads, '-', maxloads)
s1 <- apply(df, 1, paste0, collapse = '')
s2 <- paste0('MEAN = ', paste0(fnames, collapse = ','))
s3 <- paste0('COV = ', paste0(fnames, collapse = '*'))
ret <- paste0(c(s1, s2, s3), collapse = '\n')
mirt.model(ret)
}
gen_syntax_mirt(Design[1,])
gen_syntax_mirt(Design[10,])
The input to this function is a single row from the Design input to runSimulation(), so you can see here that it will work just fine. Do something similar for lavaan's syntax and you'll be set.