首页 > 解决方案 > 在 r studio 的图表中创建两条线

问题描述

我有以下数据框称为df_1

出版商 Total.North.America.Sales。 总计.欧洲.销售额。
ADX 816.87 418.74
3RD 595.07 371.27
BDTT 429.7 215.53
RRD-Y 265.22 187.72
RRD-X 253.43 163.32

我想创建一个像这样的图表https://i.ibb.co/z6Qz8rV/graphic.png

我首先尝试了这段代码:

png(file = "line_chart_2_lines.jpg")
chart1 <- plot(df_1, type = "o", col = "red", xlab = "Publisher", ylab = "Total Sales",
     main = "Sales")
lines(comp_na$`Total North America Sales`, type = "o", col = "blue")
legend(1, 110, legend = c("Europe", "N. America"),
       + col = c("red", "blue", "green"), lty = 1:1, cex = 0.9)
dev.off()

它不起作用,我无法识别语法问题。所以我试着用gglopt2

library(ggplot2)
install.packages("hrbrthemes")
library(hrbrthemes)

ggplot(df_1, aes(x=df_1$comp_na.Publisher, y=df_1$comp_eu..Total.Europe.Sales.)) +
  geom_line( color="#69b3a2", size=2, alpha=0.9, linetype=2) +
  lines(comp_na$`Total North America Sales`, type = "o", col = "blue")
  theme_ipsum() +
  ggtitle("Total Sales")

但结果是一个黑图表。我不确定出了什么问题。感谢您的帮助

标签: r

解决方案


您只需添加group = 1到 ggplot 或 geom_line aes()。

对于折线图,必须对数据点进行分组,以便知道要连接哪些点。在这种情况下,很简单——所有点都应该连接,所以 group=1。当使用更多变量并绘制多条线时,通常通过变量来对线进行分组。

参考:R 的食谱,章节:图形 Bar_and_line_graphs_(ggplot2),折线图。

尝试这个:

# Read data
df_1 <- read.table(text = 
"Publisher  Total.North.America.Sales.  Total.Europe.Sales.
ADX 816.87  418.74
3RD 595.07  371.27
BDTT    429.7   215.53
RRD-Y   265.22  187.72
RRD-X   253.43  163.32", 
sep = '\t', header = TRUE, fill = TRUE)

# Plot data
df_1 %>% 
  as_tibble() %>% 
  ggplot(aes(x = fct_reorder(Publisher, c(1:5)), group = 1)) +
  geom_line(aes(y = Total.North.America.Sales.), color = "blue") +
  geom_line(aes(y = Total.Europe.Sales.), color = "red") +
  labs (
    title = "Total Sales",
    y     = "",
    x     = ""
  )

如果您想要一个合适的图例,您需要将数据转换为长格式(使用pivot_longer())以将所有销售额集中到一个列中,然后在美学中使用组和颜色映射。

df_1 %>%
  
  # Convert to tibble
  as_tibble() %>%
  
  # Make data long
  pivot_longer(cols      = c(Total.North.America.Sales., 
                             Total.Europe.Sales.),
               names_to  = "location", 
               values_to = "value") %>% 
  
  # Order the Publishers according the value
  mutate(Publisher = Publisher %>% as_factor() %>% fct_reorder(-value)) %>%
  
  # Change order of the location (for the legend)
  mutate(location = location %>% as_factor() %>% fct_relevel("Total.North.America.Sales.",
                                                             "Total.Europe.Sales.")) %>%
  
  # Plot
  ggplot(aes(x     = Publisher, 
             color = location)) +
  geom_line(aes(y     = value, 
                group = location),
                size  = 2) +
  
  # Color
  scale_color_manual(values = c("dodgerblue", "orange")) +
  # Grid Lines
  scale_y_continuous(breaks = seq(0, 900, 100)) +
  # Y-Axis Limits
  expand_limits(y = c(0,900)) +
  
  # Labels
  labs(
    color = "",
    title = "North America x Europe",
    y     = "",
    x     = ""
  ) +
  
  # Formatting
  theme(legend.position    = "bottom",
        panel.background   = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.major.y = element_line(colour = "grey"),
        axis.ticks         = element_blank(),
        legend.key         = element_blank(),
        legend.text        = element_text(size = 10, color = "grey40"),
        plot.title         = element_text(size = 16, hjust = 0.5, color = "grey40"))

在此处输入图像描述


推荐阅读