Run the following code in your R console. states <- data.frame(state.region, state.x77) Draw aHistogram of per capita income
时间: 2023-07-15 16:11:11 浏览: 97
Sure, here's the code to draw a histogram of per capita income using the `states` dataset:
```
# Load the states dataset
data(states)
# Set up a plot area
par(mar = c(5, 4, 4, 2) + 0.1)
# Create a histogram of per capita income
hist(states$Income, main = "Per Capita Income", xlab = "Income", ylab = "Frequency", col = "blue", border = "white")
```
This will create a histogram of per capita income in the `states` dataset, with the x-axis labeled as "Income" and the y-axis labeled as "Frequency". The histogram bars will be colored blue and have a white border.
相关问题
Run the following code in your R console. states <- data.frame(state.region, state.x77) Draw a Kernel density of per capita income
Sure! Here's how you can do it:
```R
# Load the necessary library
library(ggplot2)
# Create a data frame with state region and per capita income
states <- data.frame(state.region, state.x77)
# Rename the variables
names(states) <- c("region", "per_capita_income")
# Draw a kernel density plot of per capita income
ggplot(states, aes(x = per_capita_income)) +
geom_density(fill = "blue", alpha = 0.5) +
ggtitle("Kernel density of per capita income")
```
This code will create a kernel density plot of per capita income using the `ggplot2` library in R. The plot will have a blue fill and an alpha value of 0.5 to make it more visually appealing.
Run the following code in your R console. states <- data.frame(state.region, state.x77) Draw a Kernel density of per capita income N = 50 Bandwidth = 252.1
Here's the code to draw a kernel density plot of per capita income:
```R
# Load the data
data(state.x77)
data(state.region)
# Combine the data into a data frame
states <- data.frame(state.region, state.x77)
# Set the bandwidth
bw <- 252.1
# Plot the kernel density
library(ggplot2)
ggplot(states, aes(x = Income)) +
geom_density(fill = "blue", alpha = 0.5) +
labs(title = "Kernel Density of Per Capita Income",
x = "Per Capita Income",
y = "Density") +
theme_minimal()
```
This will produce a kernel density plot of per capita income with a bandwidth of 252.1 and 50 observations. You may need to adjust the `bw` value to get the desired smoothness in the plot.
阅读全文