ggplot:将两个变量绘制成一个方面

ggplot: plotting two variables into a single facet

目标:我正在 ggplot 的帮助下研究货币兑换的基本趋势。我能够实现最初预期的想法来绘制收盘价和平均利率的月度趋势。 (收盘价 - 表示每个月末的收盘价;平均 - 表示当月迄今的平均转化率)。但是,我正在寻找建议来增强情节以获得更好的表现力。

预期:从下面所附的图表中,在单个方面可视化同一货币的收盘价和平均汇率比较更有意义。

这是可重现的示例代码:

library(ggplot2, quietly = TRUE)
library(reshape2,  quietly = TRUE)
library(data.table,  quietly = TRUE)
library(readxl,  quietly = TRUE)


#Monthly data for closing and average rates of currency conversion.

#Note: For representation purposes, I've just increased the average rates by 5% of the closing rates.

data = data.frame("Month_Name" = c("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"), 
"USD_INR_Closing" = c(69.565, 69.68, 69.02, 68.795, 71.405, 70.875, 70.925,71.745),
"EUR_USD_Closing" = c(1.1213972545, 1.1148464413, 1.1377137056, 1.1148339268, 1.1037392343, 1.0908994712, 1.1162143115, 1.1002508891),
"USD_INR_Average" = c(73.04325, 73.164, 72.471, 72.23475, 74.97525, 74.41875, 74.47125, 75.33225), 
"EUR_USD_Average" = c(1.1774671172, 1.1705887633, 1.1945993909, 1.1705756232, 1.158926196, 1.1454444447, 1.1720250271, 1.1552634336) ) 



# Performing transformation
df <- melt(data, id.vars = 'Month_Name', measure.vars = c("USD_INR_Closing" , "EUR_USD_Closing", "USD_INR_Average", "EUR_USD_Average"),
           variable.name = "Conversion", value.name = "Currency")

f <- function(x){
  format(round(x, 3), nsmall=1)
}

# Generating a plot in dersire format with N panes. (N = number of currency conversions required to display) 
ggplot(df, aes(x = factor(df$Month_Name, levels = unique(df$Month_Name)), y=Currency, group=1)) + 
  geom_line() +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5),
                                 panel.spacing = unit(0.8, "lines"), 
                                 panel.border = element_rect(color = "black", fill = NA, size = 0.8),
                                 ) +  
  facet_grid(Conversion ~ ., scales = 'free_y')+
  ggtitle("Currency Conversion Trends")+
  ylab("Conversion Rate")+xlab("Month")+
   geom_text(
       size = 3.0,
       fontface='bold',
       aes(label = sprintf("%0.2f", round(Currency, digits = 2)), y = Currency),
       vjust = +0.3,
       #nudge_y = 0.01,
       nudge_x = 0.35,
     )+
  geom_point(shape=21, color="black", fill="#69b3a2", size=3)


这是输出图:

在给定的场景中,我如何将 USD_INR_ClosingUSD_INR_Average 的折线图绘制到单个方面 (比较收盘价与平均价)并将其应用于其他货币换算?

有很多不必要的代码,而且你的代码也不起作用。我总是建议在空会话中重现它.. 先新建一个分组变量。
编辑
在考虑了我使用逻辑语句进行分组的第一种方法之后,我改变了创建不同组的方法,使用 tidyr::separate。这通常会清理您的数据,您可以更好地使用它的变量。对于分组,我使用了 interaction.

我还使用 R 常量 month.abb 分解您的“月份”列。

library(tidyverse)

mydf <- data.frame(
  "Month_Name" = c("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"),
  "USD_INR_Closing" = c(69.565, 69.68, 69.02, 68.795, 71.405, 70.875, 70.925, 71.745),
  "EUR_USD_Closing" = c(1.1213972545, 1.1148464413, 1.1377137056, 1.1148339268, 1.1037392343, 1.0908994712, 1.1162143115, 1.1002508891),
  "USD_INR_Average" = c(73.04325, 73.164, 72.471, 72.23475, 74.97525, 74.41875, 74.47125, 75.33225),
  "EUR_USD_Average" = c(1.1774671172, 1.1705887633, 1.1945993909, 1.1705756232, 1.158926196, 1.1454444447, 1.1720250271, 1.1552634336)
)

mydf2 <- pivot_longer(mydf, cols =USD_INR_Closing:EUR_USD_Average, names_to = "Conversion", values_to = "Currency")  %>% 
  mutate(Month_Name = factor(Month_Name, levels = month.abb)) %>%
  separate(Conversion, into = c('from','to','type'))

ggplot(mydf2, aes(x = Month_Name, y = Currency, group = type)) + 
  geom_line() +  
  geom_point(shape=21, color="black", fill="#69b3a2", size=3) +
  facet_wrap(~ interaction(from,to), scales = 'free_y') 

reprex package (v0.3.0)

于 2020 年 1 月 2 日创建

感谢@Tung 和@Parfait,尤其是@Tjebo 指导了正确的方向。

这是通过以下代码实现的预期输出:

library(tidyverse,  quietly = TRUE)
library(ggrepel,  quietly = TRUE)

mydf = data.frame("Month_Name" = c("Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov"), 
                  "USD_INR_Closing" = c(69.565, 69.68, 69.02, 68.795, 71.405, 70.875, 70.925,71.745),
                  "EUR_USD_Closing" = c(1.1213972545, 1.1148464413, 1.1377137056, 1.1148339268, 1.1037392343, 1.0908994712, 1.1162143115, 1.1002508891),
                  "USD_INR_Average" = c(73.04325, 73.164, 72.471, 72.23475, 74.97525, 74.41875, 74.47125, 75.33225), 
                  "EUR_USD_Average" = c(1.1774671172, 1.1705887633, 1.1945993909, 1.1705756232, 1.158926196, 1.1454444447, 1.1720250271, 1.1552634336) ) 

mydf2 <- pivot_longer(mydf, cols =USD_INR_Closing:EUR_USD_Average, names_to = "Conversion", values_to = "Currency")  %>% 
  mutate(Month_Name = factor(Month_Name, levels = month.abb)) %>%
  separate(Conversion, into = c('from','to','measure'))

ggplot(mydf2, aes(x = factor(mydf2$Month_Name, levels = unique(mydf2$Month_Name)), y = Currency, group = measure, colour = measure )) + 
  geom_line() +
  ggtitle("Currency Conversion Trends - Average vs. Closing Rates")+
  ylab("Conversion Rate")+xlab("Month")+
  geom_point(shape=21, color="black", fill="#69b3a2", size=2) +
  geom_text_repel(size = 2.8,
                  fontface='bold',
                  aes(label = sprintf("%0.2f", round(Currency, digits = 2)), y = Currency),
                  box.padding = 0.25,
                  direction = 'both' )+
  facet_wrap(~ interaction(from,to), ncol = 1, scales = 'free_y') 

输出图: