Upper cases (superscript) in ggplot (R) at x-axis? - ggplot2

How I can make x axis text like 1st, 2st, 3rd, 4th superscripted in R?
Thanks in advance.

You can simply use expression() for that.
library(tidyverse)
ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width^2)) +
geom_point() +
labs(x="Sepal.length", y=expression(Sepal.Width^2), title="Title")
ggplot(data=iris, aes(x=Sepal.Length, y=Sepal.Width^2)) +
geom_point() +
labs(x="Sepal.length", y=expression(Sepal.Width^"3rd"), title="Title")
Is that what you need?

Related

Can I use Lattice auto.key or key to make a legend with points for some data and lines for others?

I often make figures that have observed data represented as points and model-predicted data represented as lines, using distribute.type to assign graph types. Is there a way to make a legend that only shows points for the points data, and lines for the lines data? The auto.key default is points, and if I add lines with "list(lines=TRUE)" the legend shows both points and lines for every data label:
x <- seq(0, 8*pi, by=pi/6)
Y1pred <- sin(x)
Y1obs <- Y1pred + rnorm(length(x), mean=0, sd=0.2)
Y2pred <- cos(x)
Y2obs <- Y2pred + rnorm(length(x), mean=0, sd=0.4)
xyplot(Y1obs + Y2obs + Y1pred + Y2pred ~ x,
type=c('p','p','l','l'),
distribute.type=TRUE,
auto.key=list(lines=TRUE, columns=2)
)
There is a rather complicated example using 'key' on p. 158 of Deepayans' book on Lattice, I am wondering if there are simple options?
Yes, following S, the lines components of key supports different type-s (but not points). Using auto.key, you could do
xyplot(Y1obs + Y2obs + Y1pred + Y2pred ~ x,
type=c('p','p','l','l'),
distribute.type=TRUE,
auto.key = list(points = FALSE, lines = TRUE,
columns = 2,
type = c('p','p','l','l')))
Ideally, you would want to put the type only inside the lines component, and that's how you should do it if you use key. For auto.key, there can be only one line anyway, so this should be fine.

How can I adjust bars on X-axis accord to numerical order?

The ideal order of bars on X-axis is (S1,S2,S3,S4,S5,S6,S7,S8,S9,S10).But it just can not be adjust successfully :(
ggplot(data=T1, aes(x=Scenarios, y=Yields, fill=Scenarios)) +
geom_bar(stat="identity") +
scale_fill_manual(values=cbPalette)
Under the hood ggplot converts the strings to factors and the default levels will be the strings sorted alphabetically (or depending how you create T1, it could be at that point). If you want to customize the sort order, explicitly cast your x variable as a factor with the level order you prefer.
Either ahead of time:
T1 <- dplyr::mutate(T1, Scenarios = factor(Scenarios, Scenarios))
or inline with the ggplot call
ggplot(aes(x= factor(scenarios, scenarios), y= yields, fill=scenarios)) +
geom_bar(stat="identity")

Wolfram Alpha Solution Entry

Using this Wolfram Alpha code either through web or in Mathematica:
(5y^4+3y^2+e^y)y'=cos(x),y(0)=0
My equation seems to be properly parsed:
{(5 y(x)^4 + 3 y(x)^2 + e^(y(x))) y'(x) = cos(x), y(0) = 0}
As a separable DE, the result should be:
y^5 + y^3 + e^y = sin(x) + 1
How do I modify the original Wolfram Alpha code to get the program to evaluate the solution?
Using Wolfram Alpha
{(5y[x]^4+3y[x]^2+E^y[x])y'[x]==Cos[x],y[0]==0} solution

Axis width/thickness in ggtern

A simple - and possible elementary - question that I have not been able to find an answer for:
How do I change/manage the axis width/thickness in ggtern?
Looks like the usual commands for that does not apply to ggtern
Help would be highly appreciated
Thanks in advance
Lars
This is controlled by the new theme elements which are unique to ggtern, consider below some examples, which should be fairly self-explanatory:
library(ggtern)
base = ggtern() + theme_bw()
#All Axes
base + theme(tern.axis.line = element_line(color='red',size=2))
#Individual Axes
base + theme(tern.axis.line.T = element_line(color='red',size=2))
base + theme(tern.axis.line.L = element_line(color='green',size=2))
base + theme(tern.axis.line.R = element_line(color='blue',size=2))

Use GP for Solving Symbolic Regression

I understand how to solve a simple symbolic regression problem such as f(x)=x4+x3+x2+x. However, I'm not quite sure how to use GP to solve the below one.
f(x) = x2 + cosx + 7, for x<=0,
1/x + 2x, for x>0
Can anyone help with this?
It says that you compare against
f = x<=0 ? x2 + cosx + 7 : 1/x + 2x
instead of
f = x4+x3+x2+x