In this data, i would like to compute the difference between the 2 values of ax (and all columns) that correspond to 1990 and 1999.
myd=structure(list(year = c(1990, 1991, 19962, 1993, 1994, 1999),
ud = c(137.25, 135, 125, 139,
125.5, 127.5), ax = c(67, 70,
69, 71, 69, 68
)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"))
Then plot the results using ggplots as bars
The solution has two parts:
Wrangling the data is straightforward. (The code here could be shortened, but I've written it as it is to make the various steps clear.) Also note the "obvious" correction to the typo mentioned by @user2974951.
# Extract the baseline values and convert to long format
baseline <- myd %>%
filter(year == 1990) %>%
select(-year) %>%
pivot_longer(everything(), names_to="variable", values_to="baseline")
# Extract the endpoint values and convert to long format
endpoint <- myd %>%
filter(year == 1999) %>%
select(-year) %>%
pivot_longer(everything(), names_to="variable", values_to="endpoint")
# Merge by variable and calculate difference
difference <- baseline %>%
full_join(endpoint, by="variable") %>%
mutate(diff=endpoint-baseline)
At this point, difference
looks like this:
> difference
# A tibble: 2 × 4
variable baseline endpoint diff
<chr> <dbl> <dbl> <dbl>
1 ud 137. 128. -9.75
2 ax 67 68 1
Now create the bar chart.
# Create the bar chart
difference %>%
ggplot() +
geom_col(aes(x=variable, y=diff))
Note that this solution is robust with respect to the number of variables in the original dataset, and their names. It will also handle missing values without error. It could easily be generalised to calculate and plot the difference between any two years (eg earliest year as baseline and most recent as endpoint).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.