R: $ cannot be used for atomic vectors (box plot) - r

I would love some advice on how to fix the error shown in the screenshot. I only started learning R yesterday so I'm not very familiar with it. I tried using % but this produced a different type of error (unexpected input). Are there any problems with how I've defined time_spen and species earlier on in the code?

Do you want to make a boxplot of the time_spen variable for each species? The ggplot2 package can help.
Since I don't have your data, here is an example from the penguins dataset in the palmerpenguins package.
library(palmerpenguins)
library(ggplot2)
ggplot(penguins) +
geom_boxplot(aes(x = species, y = body_mass_g))

Related

ggplot error in if (node$tag == "span") after trying to split a faceted plot over multiple pages

My dataset includes the variable "annual_TRW" measured over different "survey_year", with 5 "Sample_Core_ID" within each "block" nested within each "site".
The following code was working to plot all samples for a plot subset.
pdf("plots.pdf")
library(ggplot2)
dataset %>%
group_by(site, block, Sample_Core_ID ) %>%
ggplot(., aes(x = survey_year, y = annual_TRW, colour = Sample_Core_ID)) +
geom_line() +
facet_grid(Sample_Core_ID ~ .)
dev.off()
Then, I was trying to split a faceted plot over multiple pages, with each page including the faceted plots (1 column, 5 rows) corresponding to the samples of each block. I was testing using different different functions ggplus::facet_multiple, gridExtra::marrangeGrob, and ggforce::facet_wrap_paginate, but without success i.e. pdf were created empty.
The problem is that, after trying different options, now I get the following error every time that I try to use any ggplot on the same dataset. Now, I dont manage to plot the code that worked before without giving this error.
Error in if (node$tag == "span") { : argument is of length zero
Question 1: How can I solve this error every time I try to use this ggplot?
Question 2: Once this works again, how can I split the faceted plot over multiple pages as explained before?
Thanks a lot in advance!
I tried re-running the entire code, and re-calling the packages, but it doesnt work.
Regarding Problem 1: the error was solved after updating the R version and reinstalling only the necessary packages that worked. The issue might have come from installing the packages ggplus as described here https://github.com/guiastrennec/ggplus

Ordination Graph in R

Before I begin, I am very much a beginner at R, so apologies if this is an easy fix.
I need to create an ordination graph with species labels and a key. I found the 'vegan' package which I believe is relevant. I've read in the table and attached it and also have loaded the package:
Ordination<-read.table("pHWTD.txt", header = T, stringsAsFactors = T)
attach(Ordination)
library(vegan)
ordiplot(Mean_pH ~ Mean_Water_Table_Depth)
After this ordiplot function, the console returns: "Error in NROW(X) : object 'X' not found"
I don't understand this because Mean_pH and Mean_Water_Table_Depth are variables, so unsure what is not being found.
Below is a generalised version of the data I need to plot (where each species will be represented by a graphical point with an abbreviated label e.g. Species 1 could be spec.1):
Any advice, if possible, would be appreciated.
Many thanks in advance.

Error in as.double(y) : cannot coerce type 'environment' to vector of type 'double' in ggplot command in R

I am trying to make a plot between two different variables (RodC independent varaible and TickPrev dependent varaible) using the ggplot command. I have the following packages installed: pacman, party, rio, tidyverse,dplyr,MASS.
gg1314 <- ggplot(df1314, aes(RodC, TickPrev, color=YearS)) +
geom_jitter() +
labs(title = "Tick Prevalence vs Captured Rodents 2013-2014",
x="Tick Prevalence (%)",
y="Nº of Captured Rodents") +
scale_color_discrete(name="YearSeason")
The code above worked perfectly fine, but when I deleted a few rows to conduct a similar analysis with the same variables, the code start to give the following error:
ggNBS <- ggplot(dfNBS, aes(RodC, TickPrev, colors=Year)) +
geom_jitter() +
labs(title = "Tick Prevalence vs Captured Rodents NBS",
x="Tick prevalence (%)",
y="Nº of captured rodents") +
scale_color_discrete(name="Year")
Error in as.double(y) : cannot coerce type 'environment' to vector
of type 'double'
The only difference between the dataframes is that I eliminated some rows. But the variables remained the same.
enter image description here
How can I solve this.
Thank you very much
Image from df1314. dfNBS is the same, but in the MiceS and YearS it only contains information that had NBS or NBS2.
I found the answer. I was also using %>% plot() so that the plot of the graphic was done right way. After removing that line it solved the problem. :) Thank you for the help.
However now, the plot doens't present colors, all the points are black and shouldn't be.

R: Cleaning GGally Plots

I am using the R programming language and I am new the GGally library. I followed some basic tutorials online and ran the following code:
#load libraries
library(GGally)
library(survival)
library(plotly)
I changed some of the data types:
#manipulate the data
data(lung)
data = lung
data$sex = as.factor(data$sex)
data$status = as.factor(data$status)
data$ph.ecog = as.factor(data$ph.ecog)
Now I visualize:
#make the plots
#I dont know why, but this comes out messy
ggparcoord(data, groupColumn = "sex")
#Cleaner
ggparcoord(data)
Both ggparcoord() code segments successfully ran, however the first one came out pretty messy (the axis labels seem to have been corrupted). Is there a way to fix the labels?
In the second graph, it makes it difficult to tell how the factor variables are labelled on their respective axis (e.g. for the "sex" column, is "male" the bottom point or is "female" the bottom type). Does anyone know if there is a way to fix this?
Finally, is there a way to use the "ggplotly()" function for "ggally" objects?
e.g.
a = ggparcoord(data)
ggplotly(a)
Thanks
Looks like your data columns get converted to a factor when adding the groupColumn. To prevent that you could exclude the groupColumn from the columns to be plotted:
BTW: Not sure about the general case. But at least for ggparcoord ggplotly works.
library(GGally)
library(survival)
data(lung)
data = lung
data$sex = as.factor(data$sex)
data$status = as.factor(data$status)
data$ph.ecog = as.factor(data$ph.ecog)
#I dont know why, but this comes out messy
ggparcoord(data, seq(ncol(data))[!names(data) %in% "sex"], groupColumn = "sex")

Plot histograms or pie charts in a scatter plot

I need to repeat the thing done in:
tiny pie charts to represent each point in an scatterplot using ggplot2 but I stumbled into the problem that the package ggsubplot is not available for 3.3.1 R version.
Essentially I need a histogram or a pie chart in predefined points on the scatterplot. Here is the same code that is used in the cited post:
foo <- data.frame(X=runif(30),Y=runif(30),A=runif(30),B=runif(30),C=runif(30))
foo.m <- melt(foo, id.vars=c("X","Y"))
ggplot(foo.m, aes(X,Y))+geom_point()
ggplot(foo.m) +
geom_subplot2d(aes(x = X, y = Y, subplot = geom_bar(aes(variable,
value, fill = variable), stat = "identity")), width = rel(.5), ref = NULL)
The code used libraries reshape2, ggplot2 and ggsubplot.
The image that I want to see is in the post cited above
UPD: I downloaded the older versions of R (3.0.2 and 3.0.3) and checkpoint package, and used:
checkpoint("2014-09-18")
as was described in the comment bellow. But I get an error:
Using binwidth 0.0946
Using binwidth 0.0554
Error in layout_base(data, vars, drop = drop) :
At least one layer must contain all variables used for facetting
Which I can't get around, because when I try to include facet, the following error comes up:
Error: ggsubplots do not support facetting
It doesn't look like ggsubplot is going to fix itself any time soon. One option would be to use the checkpoint package, and essentially "reset" your copy of R to a time when the package was compatible. This post suggests using a time point of 2014-09-18.

Resources