I'm trying to animate a time-series of ternary plots made with ggtern. I'd like to animate the 'time' variable but am getting the error
Error: Mapping must have 3 unique named variable pairs
# Get ternary data:
df=data.frame(x=c(.7,.1,.3),
y=c(.1,.8,.2),
z=c(.2,.1,.5), time = c(1,2,3))
# Plot with each time as a facet
ggtern::ggtern(data=df,
aes(x=x,y=y,z=z))+
geom_point()+
facet_wrap( ~time)
# Animate
ggtern::ggtern(data=df,
aes(x=x,y=y,z=z))+
geom_point()+
gganimate::transition_time(time)
I'm pretty sure the error is being thrown from ggtern, but not sure how to make it compatible with gganimate. I can't find much in the documentation to figure it out.
Related
I expect this kind of scatter plot.
However, whenever I tried to apply on my data, I get this.
I just used this code, and this is my data.
And I also confirmed they are numeric class.
ggplot(selected.df, aes(x, y))
making a right plot.
Those variables were not numeric.
Currently, I am trying to transition my graphical knowledge from the plot function in R, to the ggplot function. I have began constructing scatterplots and corresponding legends for a given data set, however I want to incorporate the function geom_polygon onto my plots using ggplot.
Specifically, I want to capture a triangular region from the origin of a scatterplot. For reproducibility, say I have the following data set:
rawdata<-data.frame(matrix(c(1,1,1,
2,1,-1,
3,-1,-1,
4,-1,1,
4,-2,2),5,3,byrow=TRUE))
names(rawdata)<-c("Town","x.coordinate","y.coordinate")
rawdata[,1]<-as.factor(rawdata[,1])
To construct a scatterplot along with a legend, I have been told to do the following:
p1<-ggplot(data=rawdata,aes(x=x.coordinate,y=y.coordinate,colour=Town,shape=Town))
+ theme_bw() + geom_point()
The result is the following:
Click here.
What I want to do now is produce a polygon. To do so, I have construct the following dataframe to use in the geom_polygon function:
geom_polygon(data=polygondata,aes(x = xa, y = ya),colour="darkslategray2",
fill = "darkslategray2",alpha=0.25)
However, when I combine this with p1, I get the following error:
Error in eval(expr, envir, enclos) : object 'Town' not found
From some messing around, I have noticed that when I omit the shape argument from the ggplot function, I can easily produce the desired output which is shown here. However, I wish to keep the shape for aesthetics.
I also get a similar problem when I try to produce arrows which connect points on the scatterplot using ggplot. However, I will address this problem after, as the root problem may be here.
Add the following to polygondata:
polygondata$Town = NA
Even though you're not using that variable in geom_polygon, ggplot expects it to be there if that column is used for an aesthetic in the main call to ggplot.
Alternatively, I think you could avoid the error if you move the aesthetic mapping in the initial plot to geom_point rather than the main ggplot call, like this:
p1 <- ggplot(data=rawdata) +
theme_bw() +
geom_point(aes(x=x.coordinate, y=y.coordinate, colour=Town, shape=Town))
In that case, you wouldn't need to add a Town column to polygondata.
I am fairly new to the ggplot function in R. Currently, I am struggling to produce a legend for a given data set that I have constructed by hand. For simplicity, suppose this was my data set:
rawdata<-data.frame(matrix(c(1,1,1,
2,1,-1,
3,-1,-1,
4,-1,1
4,-2,2),5,3,byrow=TRUE))
names(rawdata)<-c("Town","x-coordinate","y-coordinate")
rawdata[,1]<-as.factor(rawdata[,1])
Now, using ggplot, I am trying to figure out how to produce a legend on a scatterplot. So far I have done the following:
p1<-ggplot(data=rawdata,aes(x=x.coordinate,y=y.coordinate,fill=rawdata[,1]))
+geom_point(data=rawdata,aes(x=x.coordinate,y=y.coordinate))
I produce the following using the above code,
As you can see, the coordinates have been plotted and the legend has been constructed, but they are only colored black.
I learned that to color coordinates, I would have needed to use the argument colour=rawdata[,1] in the geom_point function to color in points. However, when I try this, I get the following error code:
Error: Aesthetics must be either length 1 or the same as the data (4): colour
I understand that this has something to do with the length of the vector, but as of right now, I have absolutely no idea how to tackle this small problem.
geom_point() takes a colour, not a fill. And, having passed the data into ggplot(data = ..), there's no need to then pass it into the geom_point() again.
I've also fixed an error in the creation of your df in your example.
rawdata<-data.frame(matrix(c(1,1,1,2,1,-1,3,-1,-1,4,-1,1,4,-2,2),5,3,byrow=TRUE))
names(rawdata)<-c("Town","x.coordinate","y.coordinate")
rawdata[,1]<-as.factor(rawdata[,1])
library(ggplot2)
ggplot(data=rawdata,aes(x=x.coordinate,y=y.coordinate,colour=Town)) +
geom_point()
I'm having a lot of trouble using my current dataset to create the barplot I need. It seems straightforward enough, but I am getting an error whenever I run my code.
link to my data set
some background information
Percent_Calls is calculated by Call/(Call+Noise)
Percent_Total is calculated by (Call+Noise)/(sum(Call)+sum(Noise));
PercentofCall is calculated by Percent_Calls*Percent_Total
I am trying to create a barplot (with percentages on the y axis) with CRF_Score as the x-variable and the Percent_Total values as the bars. Eventually, I would like to highlight the portion of PercentofCall in Percent_Total.
require(ggplot2)
ggplot(FD2_CAna, aes(CRF_Score, fill=Percent_Total)) + geom_bar(binwidth=0.05)
The above code usually works for me, however I am getting this error instead:
Error in unit(tic_pos.c, "mm") : 'x' and 'units' must have length > 0
I have tried using as.factor(x) as suggested in another thread, but the graph output is not what I need.
This is more along of lines of what I want, except it was made in JMP.
Sorry for the long explanation, what am I doing wrong here?
To get the similar plot to JMP you should use Percent_Total as y values and not as the fill= values and then in geom_bar() use stat="identity".
With your JMP plot it seems that Percent_Total is treated as factor and not as numeric variable - you can see it by comparing the height of bars with values 23 and 2 - they are almost the same width. If file FD2_CAna.csv is imported properly then values are numeric.
FD2_CAna<-read.csv2(file="FD2_CAna.csv",header=T,sep=",",dec=".")
ggplot(FD2_CAna, aes(CRF_Score, Percent_Total)) + geom_bar(stat="identity")
Is it possible to reorder x values using a computed y via stat_summary?
I would think that this should work:
stat_summary( aes( x = reorder( XVarName , ..y.. ) ) )
but I get the following error:
"Error: stat_summary requires the following missing aesthetics: x"
I've seen a number of your posts, and I think this may be helpful for you. When generating a plot, always save it to a unique variable
Create your plots without regard for ordering at first, until you're comfortable just creating the plots. Then, work your way into the structure of the ggplot objects to get a better understanding of what's in them. Then, figure out what you should be sorting.
plot1 <- ggplot() + ...
You can push plots to the viewport by typing out the object name that you've saved them to:
plot1
Creating a ggplot object (or variable) allows you the opportunity to review the structure of the plot. Which, incidentally, can answer a number of the questions that you've been having so far.
str(plot1)
It is still fairly simple to reorder a plot after you've saved it as a variable/object, albeit with slightly longer names:
plot$data$variable_tobe_recoded <- factor(...)