0
library(reshape2)
library(ggplot2)
library(plyr)
library(gridExtra)

Amendment   Feldjahre   ROC_twenty_year
GW(ten) 1   100
GW(ten) 2   53,53
GW(ten) 3   147,42
GW(ten) 4   96,43
GW(ten) 5   186,26
GW(ten) 6   131,60
GW(ten) 7   218,10
GW(ten) 8   160,43
GW(ten) 9   244,21
GW(ten) 10  184,07
GW(ten) 11  265,61
GW(ten) 12  203,44
GW(ten) 13  283,15
GW(ten) 14  219,33
GW(ten) 15  297,53
GW(ten) 16  232,35
GW(ten) 17  309,32
GW(ten) 18  243,02
GW(ten) 19  318,99
GW(ten) 20  251,77
GW(one) 1   100
GW(one) 2   53,53
GW(one) 3   47,42
GW(one) 4   42,90
GW(one) 5   38,84
GW(one) 6   35,17
GW(one) 7   31,84
GW(one) 8   28,83
GW(one) 9   26,10
GW(one) 10  23,64
GW(one) 11  21,40
GW(one) 12  19,38
GW(one) 13  17,54
GW(one) 14  15,88
GW(one) 15  14,38
GW(one) 16  13,02
GW(one) 17  11,79
GW(one) 18  10,67
GW(one) 19  9,67
GW(one) 20  8,75


y<-data$ROC_twenty_year
x<-data$Feldjahre

ggplot(data=data, aes(x, y, group = 1)) +
  geom_line(size = 1, alpha =.5, linetype="solid") +
  geom_point(aes(), size=4)+
  theme_minimal() +theme_bw(base_size=20)

Hello, I wanted to make two lines on a graph with a normal y-axis, but my graph looks like this. That is, the y-axis numbers are not consistent and the line blends all the points. Can anyone tell me what is wrong here?

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
  • 1
    You numbers are characters `53,53` has a comma as decimal point. First change `data$ROC_twenty_year <- sub(",", ".", data$ROC_twenty_year)` then coerce to numeric, `data$ROC_twenty_year <- as.numeric(data$ROC_twenty_year)`. Or better yet, read in the data with argument `dec=","`. – Rui Barradas Mar 05 '22 at 22:37
  • See [this SO post](https://stackoverflow.com/questions/1523126/how-to-read-data-when-some-numbers-contain-commas-as-thousand-separator). – Rui Barradas Mar 05 '22 at 22:48

3 Answers3

3

I'm guessing you should group the lines by Amendment, as well as changing the character column to numeric

data$ROC_twenty_year <- as.numeric(sub(",", "\\.", data$ROC_twenty_year))

ggplot(data=data, aes(Feldjahre, ROC_twenty_year, colour = Amendment)) +
  geom_line(size = 1, alpha = 0.5) +
  geom_point(size = 4) +
  theme_minimal() +
  theme_bw(base_size = 20)

enter image description here

Allan Cameron
  • 147,086
  • 7
  • 49
  • 87
1

Why are there commas in the ROC_twenty_year? I think the variable is not being read as a numeric value. If the commas are decimals, use "." instead of ",".

You could create a line graph as follows:

ggplot(data=data, aes(x, y, group = Amendment)) +
  geom_line(size = 1, alpha =.5, linetype="solid") +
  geom_point(aes(), size=4)+
  theme_minimal() +theme_bw(base_size=20)
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
Rabin KC
  • 47
  • 7
1
data <- mutate(data, ROC_twenty_year:=as.numeric(gsub(',',".",ROC_twenty_year)))

ggplot(data=data, aes(Feldjare, ROC_twenty_year)) +
  geom_line(size = 1, alpha =.5, linetype="solid") +
  geom_point(aes(), size=4)+
  theme_minimal() +theme_bw(base_size=20)

If you want separate lines for each Amendment, then add group=Amendment; your current code group=1 is not necessary, as the default is to treat as a single group.

langtang
  • 22,248
  • 1
  • 12
  • 27