I'm trying to graph trigonometric functions using python turtle, but it doesn't allow me to multiply a float with any built-in function (sine, cosine, tangent,.etc). I want to do Amath.sin((bx)-c)+d for the basic graphing formula. Is there a way to execute this? Thank you so much :)
Here's my current code:
enter code here
x= -2*(math.pi)
A=float(input('What is the amplitude? '))
b=float(input('What is the b-value? '))
c=float(input('What is the horizontal shift? '))
d=float(input('What is the vertical shift? '))
period=(2*(math.pi)/b)
y = A*(math.sin((period*x-c)+d))
t.penup()
t.goto(x,y)
t.pendown()
x=(-23*(math.pi)/12)
while x!= 2*(math.pi):
y = A*(math.sin)*((period*x-c)+d)
t.goto(x,y)
x = x+((math.pi)/12)
You have a syntax error in your second declaration of the y variable. You should use the same syntax as your first declaration to correct this.
enter code here
x= -2*(math.pi)
A=float(input('What is the amplitude? '))
b=float(input('What is the b-value? '))
c=float(input('What is the horizontal shift? '))
d=float(input('What is the vertical shift? '))
period=(2*(math.pi)/b)
y = A*(math.sin((period*x-c)+d))
t.penup()
t.goto(x,y)
t.pendown()
x=(-23*(math.pi)/12)
while x!= 2*(math.pi):
y = A*(math.sin((period*x-c)+d))
t.goto(x,y)
x = x+((math.pi)/12)
Related
I'm trying to write the << symbol in R but I'm not succeeding. Any tips?
plot(expression(alpha<1))
plot(expression(alpha<<1))
Error: unexpected input in "plot(expression(alpha<<"
Using unicode
plot(1, 1, xlab = bquote(alpha~"\u226A"~1))
This is the closest I am so far, how does this work for you?
plot(
x = 1:10,
y = 1:10,
main = expression(paste(alpha, "<<", 1))
)
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'm generating a mosaic plot with vcd::mosaic. But the text of the factors I was given are very long (cutting them is not an option, and with so many instances, introducing \n seems daunting), so there's an overlap in the texts, and I haven't been able to force the labels to go perpendicular to the axis.
This is what I'm trying:
a <- data.frame(x=sample(LETTERS[1:4],16,replace = TRUE),
y=rep(paste("very long label here at number", 1:4, paste=" "), 4))
mosaic(y ~ x, data= a, las= 2)
but this is what I get:
I've also tryed par(las= 2) and par(las= 3) but none of those is able to force them into vertical alignment (las= 2 works well with mosaicplot, though. It's like vcd::mosaic overrides las either as a given parameter or as a default-set in par. I've played also with par(mar), but the labels are long enough to fool that workaround.
What can I do to get readable labels?
########## EDIT TO ADD: ##########
I've also tried this, to no avail:
mosaic(y ~ x, data= a, labeling_list= list(gp_text= gpar(las= 2)))
and
mosaic(y ~ x, data= a, labeling_list= list(rot_labels = c(0,90,0,0)))
# Actually placed the "90" in the 4 positions
mosaic(y ~ x, data= a, labeling_list= list(rot_varnames = c(0,90,0,0)))
Finally found it! Key searching docs:
?labelings
?labeling_border
In order to rotate the labels
mosaic(y ~ x,
data= a,
labeling= labeling_border(rot_labels = c(90,0,0,0),
just_labels = c("left",
"center",
"center",
"center")))
Try this:
+ theme(axis.text.x=element_text(angle=-25, hjust= .1))
it will rotate labels for 25 degrees (you can just copy and paste)
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.