messed up on ylim

vector1 <- c(1,2,3,4,5)
vector2 <- c(6,7,8,9,10)
vector3 <- c("hello", "goodbye", "hi", "bye", "hey")
combine.columns <- cbind(vector1, vector2, vector3)
new.data.frame <- as.data.frame(combine.columns)
new.data.frame$ones <- 1
vector4 <- c(26, 39, 12, 5, 14)
new.data.frame$vector4 <- vector4
colnames(new.data.frame) <- c("col1", "col2", "col3", "col4", "col5")

new.data.frame
#---------------------------------------------------------------

Now we would like to visualize our data

to do this, we can use "Base R" which are the functions

that come automatically with R, or we can use other packages

first we will use Base R to make a bar plot

barplot(new.data.frame$col5) # make a simple barplot of col5 values

what about col3 (x) and col5 (y), for this we use "~"

barplot(new.data.frame$col5 ~ new.data.frame$col3)

lets give it a title and change the x and y labels

barplot(new.data.frame$col5 ~ new.data.frame$col3,
main = "this is the plot title",
xlab = "this is the x-label",
ylab = "this is the y-label")

one of our bars seems to be extending over the y limitation

lets change the y limitation to 50 instead

barplot(new.data.frame$col5 ~ new.data.frame$col3,
main = "this is the plot title",
xlab = "this is the x-label",
ylab = "this is the y-label",
ylim = c(0,50))

#now lets use a simple plot function to see what it does
plot(new.data.frame$col5 ~ new.data.frame$col3,
main = "this is the plot title",
xlab = "this is the x-label",
ylab = "this is the y-label",
ylim = c(0,50))

I'm not sure what your question is. The last plot() function in your code throws an error because new.data.frame$col3 is text and it seems the plot() function does not take text values. You can reproduce this error with this code:

plot(x = c("a","b","c"), y = 1:3)
Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf
1 Like

This is the error I got.
#now lets use a simple plot function to see what it does

plot(new.data.frame$col5 ~ new.data.frame$col3,

  •  main = "this is the plot title",
    
  •  xlab = "this is the x-label",
    
  •  ylab = "this is the y-label",
    
  •  ylim = c(0,50))
    

Error in plot.window(...) : need finite 'xlim' values
In addition: Warning messages:
1: In xy.coords(x, y, xlabel, ylabel, log) : NAs introduced by coercion
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

I am taking an online class, but the instructor isn't answering questions

This error is due to new.data.frame$col3 being text values. The plot function tries to calculate min(x) and max(x) but those functions return either -Inf or Inf because there are no numbers in col3. You can try

max(new.data.frame$col3)

to confirm that. If that makes sense to you, then you have probably learned all the instructor expects you to get from running that function.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.