problems with Hmisc-labelled objects in ggplot - r

I get the message
Error:no applicable method for 'round_any' applied to an object of
class "labelled"
when I try to plot my graphs using ggplot2 and R. I have labelled my variables in my data frame using Hmisc::label and I think this is the problem. How do I solve this issue?
My labels look like this:
label(data$results_lp)="Lumbure Puncture Results"
label(data$hiv_test)="HIV Test done"
label(data$outcome)="Outcome at Discharge"
label(data$vac_10mnth_complete)="Vaccinne 10months complete"
label(data$vac_3mnth_complete)="Vaccine 3months complete"
label(data$vac_uptodate)="Vaccine up to date"
label(data$dx1_pneum_rcd)="Pneumonia Recorded"
label(data$mal)="Malaria"
label(data$dx1_malaria)="Documented Malaria"
label(data$dehydrat)="Dehydration"
How do I solve this?

Remove the labels for plotting:
library(Hmisc)
DF <- data.frame(x=factor(rep(1:2,5)),y=1:10)
label(DF$x)="xLab"
label(DF$y)="yLab"
library(ggplot2)
ggplot(DF,aes(x=x,y=y)) + geom_boxplot()
#Don't know how to automatically pick scale for object of type labelled. Defaulting to continuous
ggplot(DF,aes(x=factor(unclass(x)),y=unclass(y))) + geom_boxplot()
#no warning
Unfortunately you don't give the details necessary to reproduce your error and give a customized solution.

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

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")

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

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))

Weird ggplot2 error: Empty raster

Why does
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(1.5,1.5)),aes(x=x,y=y,color=z)) +
geom_point()
give me the error
Error in grid.Call.graphics(L_raster, x$raster, x$x, x$y, x$width, x$height, : Empty raster
but the following two plots work
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(2.5,2.5)),aes(x=x,y=y,color=z)) +
geom_point()
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(1.5,2.5)),aes(x=x,y=y,color=z)) +
geom_point()
I'm using ggplot2 0.9.3.1
TL;DR: Check your data -- do you really want to use a continuous color scale with only one possible value for the color?
The error does not occur if you add + scale_fill_continuous(guide=FALSE) to the plot. (This turns off the legend.)
ggplot(data.frame(x=c(1,2), y=c(1,2), z=c(1.5,1.5)), aes(x=x,y=y,color=z)) +
geom_point() + scale_color_continuous(guide = FALSE)
The error seems to be triggered in cases where a continuous color scale uses only one color. The current GitHub version already includes the relevant pull request. Install it via:
devtools::install_github("hadley/ggplot2")
But more probably there is an issue with the data: why would you use a continuous color scale with only one value?
The same behaviour (i.e. the "Empty raster"error) appeared to me with another value apart from 1.5.
Try the following:
ggplot(data.frame(x=c(1,2),y=c(1,2),z=c(0.02,0.02)),aes(x=x,y=y,color=z))
+ geom_point()
And you get again the same error (tried with both 0.9.3.1 and 1.0.0.0 versions) so it looks like a nasty and weird bug.
This definitely sounds like an edge case better suited for a bug report as others have mentioned but here's some generalizable code that might be useful to somebody as a clunky workaround or for handling labels/colors. It's plotting a rescaled variable and using the real values as labels.
require(scales)
z <- c(1.5,1.5)
# rescale z to 0:1
z_rescaled <- rescale(z)
# customizable number of breaks in the legend
max_breaks_cnt <- 5
# break z and z_rescaled by quantiles determined by number of maximum breaks
# and use 'unique' to remove duplicate breaks
breaks_z <- unique(as.vector(quantile(z, seq(0,1,by=1/max_breaks_cnt))))
breaks_z_rescaled <- unique(as.vector(quantile(z_rescaled, seq(0,1,by=1/max_breaks_cnt))))
# make a color palette
Pal <- colorRampPalette(c('yellow','orange','red'))(500)
# plot z_rescaled with breaks_z used as labels
ggplot(data.frame(x=c(1,2),y=c(1,2),z_rescaled),aes(x=x,y=y,color=z_rescaled)) +
geom_point() + scale_colour_gradientn("z",colours=Pal,labels = breaks_z,breaks=breaks_z_rescaled)
This is quite off-topic but I like to use rescaling to send tons of changing variables to a function like this:
colorfunction <- gradient_n_pal(colours = colorRampPalette(c('yellow','orange','red'))(500),
values = c(0:1), space = "Lab")
colorfunction(z_rescaled)

Resources