Creating a Line Graph using ggplot - r

I am brand new at using R code. I am trying to produce a line graph. Here is my code:
ggplot(data = legosets, mapping = aes(x = num_sets, y = Year)) +
geom_line()
Unfortunately it is producing an error which reads
Error in FUN(X[[I]], ...) : object 'num_sets' not found Calls: ... -> f -> scales_add_defaults -> apply -> FUN execution halted
Any idea how to fix this?

Assuming this is about the legosets data in the lego package : https://github.com/seankross/lego/blob/master/data-tidy/legosets.csv
You can find the variables in there here: https://www.rdocumentation.org/packages/lego/versions/0.1.1/topics/legosets
I assume you need to replace num_sets by Pieces, but you need to read the variable dexcription yourself to confirm that.

Related

Histogram runs into object not found error

I am a student who is quite new to R and am having difficulty linking my dataset to the actual workspace. In particular, I am trying creating a histogram to show what life expectancy looks like across zipcodes of a state, but nothing is showing up.
This is what my code looks like:
install.packages("ggplot2")
library(ggplot2)
ggplot(data = df_mo, aes(x = life_expectancy)) + geom_histogram(color = "tomato")
Here is what my error message in the console states:
>ggplot(data = df_mo, aes(x = life_expectancy)) + geom_histogram(color = "tomato")
Error in FUN(X[[i]], ...) : object 'life_expectancy' not found
>
Here is what my dataset looks like:
This may be quite an elementary problem I imagine but I don't have a clue and have been at this for an hour. I've tried to look this problem up but everything i've seen has some additional bells and whistles added to the code or they are receiving a different error message.
Thank you in advance.
The problem is that the dataframe doesn't have the columns names that you want. As it is shown in the picture the names are V1, V2, .... Something like:
colnames(df_mo) = df_mo[1,]
df_mo = df_mo[-1,]
should do the trick. Also should re-consider the way you are loading the data to R so it uses the first line as column names

object not found error in R when trying to use a command line argument

I tried to pass a number on a command line to a very simple R script to do some plotting.
I got this plot.r file:
args<-commandArgs(TRUE)
vmcn<-as.integer(args[1])
library(ggplot2)
library(grid)
file<-read.table("my file.txt",header=F)
ggplot(file,aes(x=V1))+geom_histogram(binwidth=1,aes(y=..count../vmcn*100))+theme_bw()
ggsave(filename="myfile.pdf",width=4,height=4)
When I run it this way:
Rscript plot.r 5000
I got the error:
Error in eval(expr, envir, enclos) : object 'vmcn' not found
Calls: print ... <Anonymous> -> as.data.frame -> lapply -> FUN -> eval
Execution halted
Can someone enlighten me on what's wrong?
This actually has nothing to do with the fact that you run R via command line. Your script also doesn't work interactively. This is to do with the way the aes function of ggplot2 searches for your objects. Rather surprisingly, it does not by default search in the global environment. Here's how you fix it:
# some reproducible data
set.seed(1)
vmcn <- 100
file <- data.frame(V1 = rnorm(1000))
# and the fixed plotting command
ggplot(file, aes(x=V1)) +
geom_histogram(binwidth=1, aes(y=..count..*100/get("vmcn", envir=.GlobalEnv))) +
theme_bw()

error with ggplot2 pie chart

I'm using the following code to create a pie chart with ggplot2, which contains two pie charts next to one another: one for each value of "MotT". Each pie chart need to how the proportions for each "Model". Here is my code:
library(ggplot2)
library(sqldf)
df <- data.frame("MorT" = c(1,2,1,2), "Model" = c(1,1,2,2),
"Values" = c(length(outOfTime1withIns[,1]),
length(outOfMem1withIns[,1]),
length(outOfTime1noIns[,1]),
length(outOfMem1noIns[,1])))
df=sqldf("select Values,
CASE WHEN MorT==1 THEN 'Insuficient Time'
WHEN MorT==2 THEN 'Insuficient Memory'
END MorT,
CASE WHEN Model==1 THEN '1) FSM1 with Insertion Dominance'
WHEN Model==2 THEN '2) FSM1 without Insertion Dominance'
END Model from df")
p = ggplot(data=df,
aes(x=factor(1),
y=Summary,
fill = factor(Model)
)
)
I get the following error after I try to run "df=sqldf("select..."
Error in sqliteExecStatement(con, statement, bind.data) :
RS-DBI driver: (error in statement: near "Values": syntax error)
And of course p is empty. I get
Error: No layers in plot
If I try to call it.
Any help will be very much appreciated!Thanks
'Values' is a keyword in SQL so you can't use it as a variable name. Change it to 'value' or something else in the data frame, that should sort the SQL error.
It looks like you're following the example on http://www.r-chart.com/2010/07/pie-charts-in-ggplot2.html.
Firstly, you have y = Summary in your ggplot, that needs to be updated to 'value' for your code.
Next, there seemed to be an issue with the data you're using (I don't have outOfMem1noIns so i made test data), but you should make sure the values for each MorT sum up to 1.
Then the code as it is on the tutorial page should work (maybe with some warning messages...)
The SQL statement has a syntax error, as the error states. In addition, the ggplot2 error comes from the fact that you have not added a geometry, e.g. geom_point:
p = ggplot(data=df,
aes(x=factor(1),
y=Summary,
fill = factor(Model)
)
) + geom_point()

Error ploting sequences in TraMineR R

Just starting to experiment TraMiner, after having read the (very good) User Guide
I managed to create the sequences from my data, but as I'm trying to plot I got the following errors:
> seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)
Error in do.call(plot, args = plist) :
'what' must be a string or a function
Where does this come from and what can I do about it?
I think you get an error because you override the plot function. This reproduce the error:
plot <- 1
do.call(plot,list(0))
Error in do.call(plot, list(0)) :
'what' must be a character string or a function
This should ork:
rm(plot)
seqiplot(my.data$sequences, title="My first sequences", withlegend = TRUE)
I guess the error shows up because you restricted your sequence state object to the single variable my.data$sequences. Have you tried my.data instead?

ggplot "error in rename"

update I have posted my solution below, the culprit was my own rename function that overrode reshape::rename
I have been using the ggplot R package with little trouble until today. Today, I get an error using code that has previously worked, and when I debug it to the minimal working example, it still gives an error;
If I do this:
library(ggplot2)
d<- data.frame(x=1:3,y=1:3)
ggplot(data=d) + geom_line(aes(x,y))
The following error is returned:
Error in rename(x, .base_to_ggplot) :
unused argument(s) (.base_to_ggplot)
The traceback is:
6: rename(x, .base_to_ggplot)
5: rename_aes(aes)
4: aes()
3: structure(list(data = data, layers = list(), scales = Scales$new(),
mapping = mapping, options = list(), coordinates = CoordCartesian$new(),
facet = FacetGrid$new(), plot_env = environment), class = "ggplot")
2: ggplot.data.frame(data = d, aes = c(x, y))
1: ggplot(data = d, aes = c(x, y))
The error does not occur after removing all objects using rm(list=ls()), but it is still not clear to me what object is causing this error or why - how can I figure this out?
Does anyone know what may have gone wrong?
I'm not able to return the same error message that you've posted above. When running your code snippet, I'm getting the following error:
Error: geom_pointrange requires the following missing aesthetics: ymin, ymax
Accordingly, geom_pointrange() is expecting arguments for ymin and ymax. I'll leave it up to you to fill in your pertinent information for what should go into those parameters, but this code executes:
ggplot(data=d) + geom_pointrange(aes(x,y, ymin = y - .5, ymax = y + .5))
The problem is caused because ggplot2 doesn't use namespaces - this will be fixed in the next release.
The error was caused by one of the objects (thanks to pointers from #Chase).
Here is how I debugged and found the culprit. The important part was to use the try() function that keeps the for loop running despite errors
foo <- ls() #get a static list of all suspect objects
for(i in 1:length(foo)) {
print(foo[i])
rm(list=foo[i])
try(ggplot()+geom_point(aes(x=1:2,y=1:2)))
}
This resulted in the following output:
...
[1] "45 reg.model"
Error in rename(x, .base_to_ggplot) :
unused argument(s) (.base_to_ggplot)
[1] "46 reg.parms"
Error in rename(x, .base_to_ggplot) :
unused argument(s) (.base_to_ggplot)
[1] "47 rename"
[1] "48 samples"
...
aha! it was my own function rename that caused the error, since ggplot2 relies on reshape::rename.
Solution: rename the new rename function... how to prevent this in the future? Perhaps study up on the use of namespaces.

Resources