-4

I have simple DATA:

DATA1   DATA2   DATA3
1        20%    25%
2        27%    32%
3        40%    28%
4        37%    24%
5        42%    20%
6        45%    19%
7        70%    20%

As result, I want to build PLOT x = DATA1 and 2 lines with data labels like 1

In the best way, I want to control шт which intersection should be set label with the percentage. Thx

MLavoie
  • 9,671
  • 41
  • 36
  • 56
  • Welcome to Stack Overflow! Could you make your problem reproducible by sharing a sample of your data so others can help (please do not use `str()`, `head()` or screenshot)? You can use the [`reprex`](https://reprex.tidyverse.org/articles/articles/magic-reprex.html) and [`datapasta`](https://cran.r-project.org/web/packages/datapasta/vignettes/how-to-datapasta.html) packages to assist you with that. See also [Help me Help you](https://speakerdeck.com/jennybc/reprex-help-me-help-you?slide=5) & [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269) – Tung Oct 24 '18 at 06:47
  • You have data in "wide" format. You will need to convert it to a [long format](https://stackoverflow.com/questions/23420961/plotting-multiple-lines-from-a-data-frame-with-ggplot2) at which point plotting is as trivial is mapping the new variable which denotes the origin of data to color, group, fill... – Roman Luštrik Oct 24 '18 at 07:27

2 Answers2

0

You need to combine DATA2 and DATA3 to one vector and create a vector to define your data.

library(ggplot2)
x1 <- c(0.2, 0.27, 0.4, 0.37, 0.42, 0.45, 0.70)
x2 <- c(0.25, 0.32, 0.28, 0.24, 0.20, 0.19, 0.20)

data <- data.frame(x = rep(c(1:7), 2), label = rep(c("x1", "x2"),  each = 7), y = c(x1, x2))


ggplot(data = data, aes(x = x)) +
  geom_line(data = data, aes(x = x, y = y, col = label))+
  geom_text(data = data, aes(y = y, label = paste(y*100, "%", sep = "")))
gc_
  • 111
  • 5
0

It would help if you provided a reproducible example, but I presume you want something like this:

data <- data.frame(
  DATA1 = 1:7,
  DATA2 = c(20, 27, 40, 37, 42, 45, 70), 
  DATA3 = c(25,32,28,24,20,19,20))

ggplot(data=data, aes(DATA1, DATA2)) +
    geom_line() + 
    geom_label (label = DATA2) +
    geom_line (data=data, aes(DATA1, DATA3)) +
    geom_label (data=data, aes(DATA1, DATA3), label=DATA3) +
    ylab ("%")
Tom Newton
  • 199
  • 2
  • 14