plot(x,y) Error in plot.window(...) : need finite 'xlim' values

good afternoon, please your help.
Im triying graph, two columns of one datasets that containing 4 columns. But, when i run RStudio to graph, appear the next mistake.

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

The cod is very simple
x=DataSetsCorto$Epicentro
y=DataSetsCorto$Magnitud

plot(x,y)

and the datasets is this
Latitud Longitud Profundidad Magnitud Epicentro Distancia
-31.815 -69.789 165.5 3.6 Mina Los Pelambres 75
-30.254 -71.248 56.4 2.8 Andacollo 16
-37.546 -71.228 159.3 3.7 Antuco 46
-23.908 -67.261 254.2 3.5 Socaire 73
-38.800 -72.872 28.9 2.5 Temuco 25
-37.478 -74.437 23.3 3.3 Lebu 70
-21.518 -68.977 103.1 3.0 Quillagua 60
-33.655 -72.045 25.7 3.3 Navidad 39
-33.677 -72.021 27.1 3.2 Navidad 35
-21.617 -68.480 137.5 3.2 Ollagüe 50
-34.510 -72.357 19.4 2.5 Pichilemu 35
-45.572 -76.723 10.0 3.5 Melinka 30
-31.732 -71.749 22.6 2.8 Los Vilos 30
-24.145 -67.581 233.2 4.2 Socaire 69
-35.624 -72.934 15.0 2.9 Constitución 57
-38.634 -75.285 30.8 2.9 Tirúa 15

Hi Ruben, welcome!

You are not telling us what kind of plot you are trying to do, the error you are getting is because you are passing a character variable for the x-axis.

When posting questions here you are supposed to provide a REPRoducible EXample (reprex), since it seems like you are new to R, I'm going to make one for you this time. Is this close to the plot you want to do?

DataSetsCorto <- data.frame(stringsAsFactors=FALSE,
                            Latitud = c(-31.815, -30.254, -37.546, -23.908, -38.8, -37.478, -21.518,
                                        -33.655, -33.677, -21.617, -34.51, -45.572, -31.732,
                                        -24.145, -35.624, -38.634),
                            Longitud = c(-69.789, -71.248, -71.228, -67.261, -72.872, -74.437,
                                         -68.977, -72.045, -72.021, -68.48, -72.357, -76.723,
                                         -71.749, -67.581, -72.934, -75.285),
                            Profundidad = c(165.5, 56.4, 159.3, 254.2, 28.9, 23.3, 103.1, 25.7, 27.1,
                                            137.5, 19.4, 10, 22.6, 233.2, 15, 30.8),
                            Magnitud = c(3.6, 2.8, 3.7, 3.5, 2.5, 3.3, 3, 3.3, 3.2, 3.2, 2.5, 3.5,
                                         2.8, 4.2, 2.9, 2.9),
                            Epicentro = c("Mina Los Pelambres", "Andacollo", "Antuco", "Socaire",
                                          "Temuco", "Lebu", "Quillagua", "Navidad", "Navidad",
                                          "Ollagüe", "Pichilemu", "Melinka", "Los Vilos", "Socaire",
                                          "Constitución", "Tirúa"),
                            Distancia = c(75, 16, 46, 73, 25, 70, 60, 39, 35, 50, 35, 30, 30, 69, 57,
                                          15)
)

library(ggplot2)

ggplot(DataSetsCorto, aes(reorder(Epicentro, Magnitud), Magnitud)) +
    geom_point() +
    theme(axis.text.x = element_text(angle=45, hjust=1, vjust = 1))

Created on 2019-06-20 by the reprex package (v0.3.0)

1 Like

@andresrcs beat me to the punch haha. I'll share my findings in hopes of covering your options. I will cover bar plots :smile:

library(tidyverse)

DataSetsCorto <- data.frame(stringsAsFactors=FALSE,
       Latitud = c(-31.815, -30.254, -37.546, -23.908, -38.8, -37.478, -21.518,
                   -33.655, -33.677, -21.617, -34.51, -45.572, -31.732,
                   -24.145, -35.624, -38.634),
      Longitud = c(-69.789, -71.248, -71.228, -67.261, -72.872, -74.437,
                   -68.977, -72.045, -72.021, -68.48, -72.357, -76.723,
                   -71.749, -67.581, -72.934, -75.285),
   Profundidad = c(165.5, 56.4, 159.3, 254.2, 28.9, 23.3, 103.1, 25.7, 27.1,
                   137.5, 19.4, 10, 22.6, 233.2, 15, 30.8),
      Magnitud = c(3.6, 2.8, 3.7, 3.5, 2.5, 3.3, 3, 3.3, 3.2, 3.2, 2.5, 3.5,
                   2.8, 4.2, 2.9, 2.9),
     Epicentro = c("Mina Los Pelambres", "Andacollo", "Antuco", "Socaire",
                   "Temuco", "Lebu", "Quillagua", "Navidad", "Navidad",
                   "Ollagüe", "Pichilemu", "Melinka", "Los Vilos", "Socaire",
                   "Constitución", "Tirúa"),
     Distancia = c(75, 16, 46, 73, 25, 70, 60, 39, 35, 50, 35, 30, 30, 69, 57,
                   15)
) %>% mutate(id = dplyr::row_number()) # ill explain this below

x = DataSetsCorto$Epicentro
y = DataSetsCorto$Magnitud

barplot(y, names.arg = x)

To cover the base R plot, I found it easiest to use the barplot command. However, we lose a lot of information we more than likely want. ggplot2 is a great highly customization plotting package that is in the tidyverse. Sticking with bar plots we can make a simple bar plot as so:

ggplot(DataSetsCorto, aes(x = Epicentro, y = Magnitud)) +
  geom_col()

Due to ggplot's behavior with non-independent data ggplot has added two cities together. That is why I added a new column on the original data that numbered every row. We can use this new column as a grouping variable so ggplot knows to treat each row independently in terms of the x-axis. Now we can add some colors and aesthetics to spruce things up. I also added in a coord_flip to make the names easier to read (You can also tilt the x-axis labels as shown in the reply above). Now we can see a single bar per row while it is contained under the Epicentro variable.

ggplot(DataSetsCorto, mapping = aes(x = reorder(Epicentro, -Magnitud), y = Magnitud, group = id)) +
  geom_col(position = "dodge", alpha = 0.8, color = "white") +
  coord_flip()

Created on 2019-06-20 by the reprex package (v0.3.0)

first, apologize me for not askin the question rigtly.
Im going to tray explain better the situation. Im goning to explain step by step.

  1. I have a datasets with 16.000 records. The datasets that you saw before, is just one extract. This datasets, is in excel. I cleaned the data, i erased the spaces and the NA. I fixed the format, etc.
  2. later, upload the datasets in R.
  3. later, i used the next cod to visualized the plot

x=DataSetsCorto$Epicentro
y=DataSetsCorto$Magnitud

plot(x,y,type="p")

and, the result of this is the next

Error in plot.window(...) : se necesitan valores finitos de 'xlim'
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 hope so been explain better...thanks you very much

There are several kinds of plots, which one are you trying to make?

to probe the datasets, i want to graph in a plane, points, with the Epicentro en the axis X and the Magnitud in axis Y....Thanks for your help....

Like the plot on my previous post?

r4Yfzzd

Or do you mean how to do this kind of plot using base R instead of ggplot2?

Nota: Si estas teniendo problemas para expresar tu problema en ingles, puedes contactarme por mensaje directo para conversar en español.

yes...exactly....the original problem, I was trying to run an app with an R library called Shiny, using this datasets, but when i was ejecuted, this was failed. Finally, the problem was in the datasets, so im proving the file triying difrent kinf of graph.

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