I am creating a simple ternary plot.
ggtern(data=data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7)),aes(x,y,z)) + geom_point()
How can I jitter the point so that the plot will display two points?
I tried using position_jitter_tern like so: but it isn't changing anything.
ggtern(data=data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7)),aes(x,y,z, position_jitter_tern(0.1,0.1,0.1))) + geom_point()
Image can be seen here
You need to use the "position" option inside the geom_point function.
library(ggtern)
df <- data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7))
ggtern(data=df, aes(x,y,z) ) +
geom_point(position= position_jitter_tern(x=0.1, y=0.1, z=0.02))
You can apply base jitter function to the dataframe.
library(ggtern)
library(ggplot2)
data=data.frame(x=c(0.1,0.1),y=c(0.2,0.2),z=c(0.7,0.7))
data[] <- lapply(data, jitter, 3)
ggtern(data,aes(x,y,z)) + geom_point()
Related
I have a CSV file that's a grid.
Link to the raw csv file on github
I want to plot this using ggplot so that it will look like this plot.
Where the colours are representing the values in the cells. (The provided csv has other values)
I can't get it to work without using a x and y aesthetic. Maybe it can be fixed with using rownames and colnames, but now the first row and column are used as colnames and rownames. Need to find something to change that as well.
I feel like I'm going nuts. Want to use geom_bin2d() or even stat_density_2d() but couldn't get is to work.
Maybe you can try to melt it and then geom_raster():
library(ggplot2)
library(reshape2)
x = read.csv("https://raw.githubusercontent.com/Friends-of-Tracking-Data-FoTD/LaurieOnTracking/master/EPV_grid.csv",header=FALSE)
ggplot(melt(as.matrix(t(x))), aes(Var1,Var2, fill=value)) +
geom_raster() +
scale_fill_viridis_c(direction=-1) +
theme_minimal()
Or:
ggplot(melt(as.matrix(t(x))), aes(Var1,Var2, fill=value)) +
geom_tile() +
scale_fill_viridis_c(direction=-1) +
theme_minimal()
Trying to add geom_points to an autolayer() line ("fitted" in pic), which is a wrapper part of autoplot() for ggplot2 in Rob Hyndmans forecast package (there's a base autoplot/autolayer in ggplot2 too so same likely applies there).
Problem is (I'm no ggplot2 expert, and autoplot wrapper makes it trickier) the geom_point() applies fine to the main call, but how do I apply similar to the autolayer (fitted values)?
Tried type="b" like normal geom_line() but it's not an object param in autolayer().
require(fpp2)
model.ses <- ets(mdeaths, model="ANN", alpha=0.4)
model.ses.fc <- forecast(model.ses, h=5)
forecast::autoplot(mdeaths) +
forecast::autolayer(model.ses.fc$fitted, series="Fitted") + # cannot set to show points, and type="b" not allowed
geom_point() # this works fine against the main autoplot call
This seems to work:
library(forecast)
library(fpp2)
model.ses <- ets(mdeaths, model="ANN", alpha=0.4)
model.ses.fc <- forecast(model.ses, h=5)
# Pre-compute the fitted layer so we can extract the data out of it with
# layer_data()
fitted_layer <- forecast::autolayer(model.ses.fc$fitted, series="Fitted")
fitted_values <- fitted_layer$layer_data()
plt <- forecast::autoplot(mdeaths) +
fitted_layer +
geom_point() +
geom_point(data = fitted_values, aes(x = timeVal, y = seriesVal))
There might be a way to make forecast::autolayer do what you want directly but this solution works. If you want the legend to look right, you'll want to merge the input data and fitted values into a single data.frame.
I am using the following code to plot a stacked area graph and I get the expected plot.
P <- ggplot(DATA2, aes(x=bucket,y=volume, group=model, fill=model,label=volume)) + #ggplot initial parameters
geom_ribbon(position='fill', aes(ymin=0, ymax=1))
but then when I add lines which are reading the same data source I get misaligned results towards the right side of the graph
P + geom_line(position='fill', aes(group=model, ymax=1))
does anyone know why this may be? Both plots are reading the same data source so I can't figure out what the problem is.
Actually, if all you wanted to do was draw an outline around the areas, then you could do the same using the colour aesthetic.
ggplot(DATA2, aes(x=bucket,y=volume, group=model, fill=model,label=volume)) +
geom_ribbon(position='fill', aes(ymin=0, ymax=1), colour = "black")
I have an answer, I hope it works for you, it looks good but very different from your original graph:
library(ggplot2)
DATA2 <- read.csv("C:/Users/corcoranbarriosd/Downloads/porsche model volumes.csv", header = TRUE, stringsAsFactors = FALSE)
In my experience you want to have X as a numeric variable and you have it as a string, if that is not the case I can Change that, but this will transform your bucket into a numeric vector:
bucket.list <- strsplit(unlist(DATA2$bucket), "[^0-9]+")
x=numeric()
for (i in 1:length(bucket.list)) {
x[i] <- bucket.list[[i]][2]
}
DATA2$bucket <- as.numeric(x)
P <- ggplot(DATA2, aes(x=bucket,y=volume, group=model, fill=model,label=volume)) +
geom_ribbon(aes(ymin=0, ymax=volume))+ geom_line(aes(group=model, ymax=volume))
It gives me the area and the line tracking each other, hope that's what you needed
If you switch to using geom_path in place of geom_line, it all seems to work as expected. I don't think the ordering of geom_line is behaving the same as geom_ribbon (and suspect that geom_line -- like geom_area -- assumes a zero base y value)
ggplot(DATA2, aes(x=bucket, y=volume, ymin=0, ymax=1,
group=model, fill=model, label=volume)) +
geom_ribbon(position='fill') +
geom_path(position='fill')
Should give you
Is it possible to annotate a ggplot figure with a "text" element indicating a feature of the data (variable)?
library(ggplot2)
library(datasets)
my.mean <- mean(mtcars$mpg, na.rm=T)
my.mean <- as.name(my.mean)
gplot <- ggplot(mtcars, aes(mpg))+geom_histogram()
gplot <- gplot + geom_text(aes_string(label=my.mean, y=5), size=3)
This produces something on the plot that looks like a succession of numbers. Any ideas how to resolve this?
Edit: this question is different since I am not trying to annotate each histogram bin with a value. The objective is to add one single text element to the plot.
If I understood you right, you want to add a text to your plot which is defined by another dataset, i.e. a dataset which was not given as argument to ggplot().
Solution: Pass this dataset directly to your geom_text function using data=... to use it.
library(ggplot2) library(datasets)
my.mean <- mean(mtcars$mpg, na.rm=T)
ggplot(mtcars, aes(mpg)) +
geom_histogram() +
geom_text(data=data.frame(my.mean=my.mean), aes(y=5, x=my.mean, label=my.mean), size=3)
it should work like this:
gplot <- gplot + geom_text(aes(15, 5, label="some random text"))
gplot
with the numbers you can specify the location within your grid.
My goal is to visualize some data frames with ggplot2.
I have several data.frames looking like this
And my goal is a boxplot looking like this, just nicer.
I managed to get single boxplots using
plt <- ggplot(data, aes(RF, data$RF)) +
geom_boxplot()
plt
But that's not what I want.
library(ggplot2)
library(reshape)
airquality_m = melt(airquality)
ggplot(airquality_m, aes(variable, value )) + geom_boxplot()
I did not beautify the plot but I guess you get the idea here.
That boxplot you showed is created with base-r graphics. Single command
boxplot(data) will do it.
If you want to use ggplot, you have to first melt the dataframe and then plot.
library(reshape2)
datPlot <- melt(data)
ggplot(datPlot,aes(variable,value)) + geom_boxplot()
I guess this is what you want:
library(ggplot2)
library(reshape)
myddt_m = melt(mydata)
names(myddt_m)=c("Models","CI")
ggplot(myddt_m, aes(Models, CI,fill=Models )) + geom_boxplot()+guides(fill=FALSE)+labs( x="", y="C-Index")