Reputation: 37
I'm trying to filter my data based on some criteria before plotting in ggplot2
.
When I plotted my entire dataset with the following command, everything worked as expected:
x_in %>%
ggplot(aes(x = x_in$`Turbine Wind Speed Adjusted m/s (m/s)`,
y = x_in$`Turbine Power KW (kW)`)) +
geom_point()
However, when I filtered the data before plotting it, I got an error:
x_in %>%
dplyr::filter(`Turbine Wind Speed Adjusted m/s (m/s)`,
`Turbine Power KW (kW)` < 1700) %>%
ggplot(aes(x = x_in$`Turbine Wind Speed Adjusted m/s (m/s)`,
y = x_in$`Turbine Power KW (kW)`)) +
geom_point()
Error: Aesthetics must be either length 1 or the same as the data (34812): x, y
Why do I get this error when I filter my data before plotting?
Here's the first 150 rows of my dataset using dput
:
dput(x_in[1:150,])
structure(list(`Local Period (ending)` = c("1/1/2018 12:00:00 AM",
"1/1/2018 12:10:00 AM", "1/1/2018 12:20:00 AM", "1/1/2018 12:30:00 AM",
"1/1/2018 12:40:00 AM", "1/1/2018 12:50:00 AM", "1/1/2018 1:00:00 AM",
"1/1/2018 1:10:00 AM", "1/1/2018 1:20:00 AM", "1/1/2018 1:30:00 AM",
"1/1/2018 1:40:00 AM", "1/1/2018 1:50:00 AM", "1/1/2018 2:00:00 AM",
"1/1/2018 2:10:00 AM", "1/1/2018 2:20:00 AM", "1/1/2018 2:30:00 AM",
"1/1/2018 2:40:00 AM", "1/1/2018 2:50:00 AM", "1/1/2018 3:00:00 AM",
"1/1/2018 3:10:00 AM", "1/1/2018 3:20:00 AM", "1/1/2018 3:30:00 AM",
"1/1/2018 3:40:00 AM", "1/1/2018 3:50:00 AM", "1/1/2018 4:00:00 AM",
"1/1/2018 4:10:00 AM", "1/1/2018 4:20:00 AM", "1/1/2018 4:30:00 AM",
"1/1/2018 4:40:00 AM", "1/1/2018 4:50:00 AM", "1/1/2018 5:00:00 AM",
"1/1/2018 5:10:00 AM", "1/1/2018 5:20:00 AM", "1/1/2018 5:30:00 AM",
"1/1/2018 5:40:00 AM", "1/1/2018 5:50:00 AM", "1/1/2018 6:00:00 AM",
"1/1/2018 6:10:00 AM", "1/1/2018 6:20:00 AM", "1/1/2018 6:30:00 AM",
"1/1/2018 6:40:00 AM", "1/1/2018 6:50:00 AM", "1/1/2018 7:00:00 AM",
"1/1/2018 7:10:00 AM", "1/1/2018 7:20:00 AM", "1/1/2018 7:30:00 AM",
"1/1/2018 7:40:00 AM", "1/1/2018 7:50:00 AM", "1/1/2018 8:00:00 AM",
"1/1/2018 8:10:00 AM", "1/1/2018 8:20:00 AM", "1/1/2018 8:30:00 AM",
"1/1/2018 8:40:00 AM", "1/1/2018 8:50:00 AM", "1/1/2018 9:00:00 AM",
"1/1/2018 9:10:00 AM", "1/1/2018 9:20:00 AM", "1/1/2018 9:30:00 AM",
"1/1/2018 9:40:00 AM", "1/1/2018 9:50:00 AM", "1/1/2018 10:00:00 AM",
"1/1/2018 10:10:00 AM", "1/1/2018 10:20:00 AM", "1/1/2018 10:30:00 AM",
"1/1/2018 10:40:00 AM", "1/1/2018 10:50:00 AM", "1/1/2018 11:00:00 AM",
"1/1/2018 11:10:00 AM", "1/1/2018 11:20:00 AM", "1/1/2018 11:30:00 AM",
"1/1/2018 11:40:00 AM", "1/1/2018 11:50:00 AM", "1/1/2018 12:00:00 PM",
"1/1/2018 12:10:00 PM", "1/1/2018 12:20:00 PM", "1/1/2018 12:30:00 PM",
"1/1/2018 12:40:00 PM", "1/1/2018 12:50:00 PM", "1/1/2018 1:00:00 PM",
"1/1/2018 1:10:00 PM", "1/1/2018 1:20:00 PM", "1/1/2018 1:30:00 PM",
"1/1/2018 1:40:00 PM", "1/1/2018 1:50:00 PM", "1/1/2018 2:00:00 PM",
"1/1/2018 2:10:00 PM", "1/1/2018 2:20:00 PM", "1/1/2018 2:30:00 PM",
"1/1/2018 2:40:00 PM", "1/1/2018 2:50:00 PM", "1/1/2018 3:00:00 PM",
"1/1/2018 3:10:00 PM", "1/1/2018 3:20:00 PM", "1/1/2018 3:30:00 PM",
"1/1/2018 3:40:00 PM", "1/1/2018 3:50:00 PM", "1/1/2018 4:00:00 PM",
"1/1/2018 4:10:00 PM", "1/1/2018 4:20:00 PM", "1/1/2018 4:30:00 PM",
"1/1/2018 4:40:00 PM", "1/1/2018 4:50:00 PM", "1/1/2018 5:00:00 PM",
"1/1/2018 5:10:00 PM", "1/1/2018 5:20:00 PM", "1/1/2018 5:30:00 PM",
"1/1/2018 5:40:00 PM", "1/1/2018 5:50:00 PM", "1/1/2018 6:00:00 PM",
"1/1/2018 6:10:00 PM", "1/1/2018 6:20:00 PM", "1/1/2018 6:30:00 PM",
"1/1/2018 6:40:00 PM", "1/1/2018 6:50:00 PM", "1/1/2018 7:00:00 PM",
"1/1/2018 7:10:00 PM", "1/1/2018 7:20:00 PM", "1/1/2018 7:30:00 PM",
"1/1/2018 7:40:00 PM", "1/1/2018 7:50:00 PM", "1/1/2018 8:00:00 PM",
"1/1/2018 8:10:00 PM", "1/1/2018 8:20:00 PM", "1/1/2018 8:30:00 PM",
"1/1/2018 8:40:00 PM", "1/1/2018 8:50:00 PM", "1/1/2018 9:00:00 PM",
"1/1/2018 9:10:00 PM", "1/1/2018 9:20:00 PM", "1/1/2018 9:30:00 PM",
"1/1/2018 9:40:00 PM", "1/1/2018 9:50:00 PM", "1/1/2018 10:00:00 PM",
"1/1/2018 10:10:00 PM", "1/1/2018 10:20:00 PM", "1/1/2018 10:30:00 PM",
"1/1/2018 10:40:00 PM", "1/1/2018 10:50:00 PM", "1/1/2018 11:00:00 PM",
"1/1/2018 11:10:00 PM", "1/1/2018 11:20:00 PM", "1/1/2018 11:30:00 PM",
"1/1/2018 11:40:00 PM", "1/1/2018 11:50:00 PM", "1/2/2018 12:00:00 AM",
"1/2/2018 12:10:00 AM", "1/2/2018 12:20:00 AM", "1/2/2018 12:30:00 AM",
"1/2/2018 12:40:00 AM", "1/2/2018 12:50:00 AM"), `Turbine Power KW (kW)` = c(1551.46,
1552.08, 1552.7, 1553.33, 1553.95, 1554.57, 1555.19, 1555.81,
1556.43, 1557.06, 1557.68, 1558.3, 1558.92, 1559.54, 1560.17,
1560.79, 1561.41, 1562.03, 1562.65, 1563.28, 1563.9, 1564.52,
1565.14, 1565.76, 1566.38, 1567.01, 1567.63, 1568.25, 1568.87,
1569.49, 1570.12, 1570.74, 1571.36, 1571.98, 1572.6, 1573.23,
1573.85, 1574.47, 1575.09, 1575.71, 1576.34, 1576.96, 1577.58,
1578.2, 1578.82, 1579.44, 1580.07, 1580.69, 1581.31, 1581.93,
1582.55, 1583.18, 1583.8, 1584.42, 1585.04, 1585.66, 1586.29,
1586.91, 1587.53, 1588.15, 1588.77, 1589.4, 1590.02, 1590.64,
1591.26, 1591.88, 1592.5, 1593.13, 1593.75, 1594.37, 1594.99,
1595.61, 1596.24, 1596.86, 1597.48, 1598.1, 1598.72, 1599.35,
1599.97, 1600.59, 1601.21, 1601.83, 1602.45, 1603.08, 1603.7,
1604.32, 1604.94, 1605.56, 1606.19, 1606.81, 1607.43, 1608.05,
1608.67, 1609.3, 1609.92, 1610.54, 1611.16, 1611.78, 1612.41,
1613.03, 1613.65, 1614.27, 1614.89, 1615.51, 1616.14, 1616.76,
1617.38, 1618, 1618.62, 1619.25, 1619.87, 1620.49, 1621.11, 1621.73,
1622.36, 1622.98, 1623.6, 1624.22, 1624.84, 1625.46, 1626.09,
1626.71, 1627.33, 1627.95, 1628.57, 1629.2, 1629.82, 1630.44,
1631.06, 1631.68, 1632.31, 1632.93, 1633.55, 1634.17, 1634.79,
1635.42, 1636.04, 1636.66, 1637.28, 1637.9, 1638.52, 1639.15,
1639.77, 1640.39, 1641.01, 1641.63, 1642.26, 1642.88, 1643.5,
1644.12), `Turbine Wind Speed Adjusted m/s (m/s)` = c(2.53, 2.53,
2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53, 2.53,
8.76, 8.76, 8.77, 8.77, 8.77, 8.77, 8.77, 8.78, 8.78, 8.78, 8.78,
8.79, 8.79, 8.79, 8.79, 8.79, 8.8, 8.8, 8.8, 8.8, 8.81, 8.81,
8.81, 8.81, 8.81, 8.82, 8.82, 8.82, 8.82, 8.83, 8.83, 8.83, 8.83,
8.83, 8.84, 8.84, 8.84, 8.84, 8.85, 8.85, 8.85, 8.85, 8.86, 8.86,
8.86, 8.86, 8.86, 8.87, 8.87, 8.87, 8.87, 8.88, 8.88, 8.88, 8.88,
8.88, 8.89, 8.89, 8.89, 8.89, 8.9, 8.9, 8.9, 8.9, 8.9, 8.91,
8.91, 8.91, 8.91, 8.92, 8.92, 8.92, 8.92, 8.92, 8.93, 8.93, 8.93,
8.93, 8.94, 8.94, 8.94, 8.94, 8.94, 8.95, 8.95, 8.95, 8.95, 8.96,
8.96, 8.96, 8.96, 8.97, 8.97, 8.97, 8.97, 8.97, 8.98, 8.98, 8.98,
8.98, 8.99, 8.99, 8.99, 8.99, 8.99, 9, 9, 9, 9, 9.01, 9.01, 9.01,
9.01, 9.01, 9.02, 9.02, 9.02, 9.02, 9.03, 9.03, 9.03, 9.03, 9.03,
9.04, 9.04, 9.04, 9.04, 9.05, 9.05, 9.05, 9.05, 9.05, 9.06, 9.06,
9.06, 9.06, 9.07), `Turbine Expected Power KW (kW)` = c(26.24,
26.24, 26.24, 26.24, 26.24, 26.24, 26.24, 26.24, 26.24, 26.24,
26.24, 26.24, 26.24, 1674.48, 1674.48, 1674.48, 1674.48, 1674.48,
1674.72, 1674.72, 1674.72, 1674.72, 1674.96, 1674.96, 1674.96,
1674.96, 1675.2, 1675.2, 1675.2, 1675.2, 1675.2, 1675.44, 1675.44,
1675.44, 1675.44, 1675.68, 1675.68, 1675.68, 1675.68, 1675.68,
1675.92, 1675.92, 1675.92, 1675.92, 1676.16, 1676.16, 1676.16,
1676.16, 1676.16, 1676.4, 1676.4, 1676.4, 1676.4, 1676.64, 1676.64,
1676.64, 1676.64, 1676.64, 1676.88, 1676.88, 1676.88, 1676.88,
1677.12, 1677.12, 1677.12, 1677.12, 1677.12, 1677.36, 1677.36,
1677.36, 1677.36, 1677.6, 1677.6, 1677.6, 1677.6, 1677.6, 1677.84,
1677.84, 1677.84, 1677.84, 1678.08, 1678.08, 1678.08, 1678.08,
1678.32, 1678.32, 1678.32, 1678.32, 1678.32, 1678.56, 1678.56,
1678.56, 1678.56, 1678.8, 1678.8, 1678.8, 1678.8, 1678.8, 1679.04,
1679.04, 1679.04, 1679.04, 1679.28, 1679.28, 1679.28, 1679.28,
1679.28, 1679.52, 1679.52, 1679.52, 1679.52, 1679.76, 1679.76,
1679.76, 1679.76, 1679.76, 1680, 1680, 1680, 1680, 1680.24, 1680.24,
1680.24, 1680.24, 1680.24, 1680.48, 1680.48, 1680.48, 1680.48,
1680.72, 1680.72, 1680.72, 1680.72, 1680.96, 1680.96, 1680.96,
1680.96, 1680.96, 1681.2, 1681.2, 1681.2, 1681.2, 1681.44, 1681.44,
1681.44, 1681.44, 1681.44, 1681.68, 1681.68, 1681.68)), row.names = c(NA,
150L), class = "data.frame")
Upvotes: 1
Views: 158
Reputation: 12155
The way you're supposed to work with ggplot
is to pass a dataset into the data=
argument, and then use the aes
function to define the mapping between variables in the dataset and in the plot. For example, if you wanted to plot the cars
dataset, you might do this:
ggplot(cars, aes(x = speed, y = dist)) +
geom_point()
Since you provided a blank name to aes
, ggplot knows that speed
refers to a variable in the cars
object, essentially cars$speed
, not an independent object in the environment. If you wanted to, though, you could override this by passing an object to aes
:
cars2 <- cars * 2
ggplot(cars, aes(x = cars2$speed, y = dist)) +
geom_point()
Now, you'll see that you're now plotting cars$dist
vs cars2$speed
.
So, what does this have to do with you? When you pipe an object into ggplot
with the pipe operator %>%
it puts that object in the data=
argument. So:
cars %>%
ggplot(aes(x = speed, y = dist)) +
geom_point()
In your example, you take x_in
, pipe it into filter
and than pipe the output into the data=
argument of ggplot
. Note that the pipe DOES NOT edit the value of x_in
, it just passes the output down the pipe. So the saved object x_in
doesn't change. The only place the filtered version of x_in
is going in your example is into ggplot(data=
But when you set up your aes
, instead of passing bare names (which would tell ggplot
to look for those variables in the data frame you passed into data=
) you specify a specific object:
aes(x = x_in$`Turbine Wind Speed Adjusted m/s (m/s)` ...
Because you specified x_in$
, ggplot will use the `Turbine Wind Speed ... `
variable in the x_in
object in the environment which has not been filtered and, therefore, has a different number of rows than the data that ggplot
expects to be working with.
To solve this, simply remove the x_in$
from your aes
so that ggplot will correctly use the data you piped in.
Upvotes: 2