0

I want to make a slope chart for my baseline and endline data to visualize the changes. I found a code in http://r-statistics.co/Top50-Ggplot2-Visualizations-MasterList-R-Code.html for a slope chart. However, when I try it using my own data I always get this error message: "Error: Discrete value supplied to continuous scale".

There are a lot of suggestion about using the fill command to my factor variable here but I am not sure how do it in my case.

Below is the code I copied from r-statistics.

data <- read.csv(file.choose(), header=TRUE)
data
str(data)
library(ggplot2)
library(scales)
theme_set(theme_classic())

colnames(data) <- c("Province", "Baseline", "Endline")
left_label <- paste(data$Province, round(data$'Baseline'),    
sep=",")
right_label <- paste(data$Province, round(data$'Endline'),
sep=",")
data$class <- ifelse((data$'Endline' - data$'Baseline') < 0,
"red", "green")

p <- ggplot(data) + geom_segment(aes(x=1, xend=2, y='Baseline',   
yend='Endline', col=class), size=.75, show.legend=F) +
geom_vline(xintercept=1, linetype="dashed", size=.1) +
geom_vline(xintercept=2, linetype="dashed", size=.1) +
scale_color_manual(labels = c("Up", "Down"),
                 values = c("green"="#00ba38", "red"="#f8766d")) +
labs(x="", y="Mean HDDS") +
xlim(.5, 2.5) + ylim(0,(1.1*(max(data$'Baseline',  
data$"Endline"))))

p <- p + geom_text(label=left_label, y=data$'Baseline',  
x=rep(1,NROW(data)), hjust=1.1, size=3.5)
p <- p + geom_text(label=right_label, y=data$'Endline', x=rep(2,  
NROW(data)), hjust=-0.1, size=3.5)
p <- p + geom_text(label="Baseline", x=1, y=1.1
(max(data$'Baseline', data$'Endline')), hjust=1.2, size=5)
p <- p + geom_text(label="Endline", x=2, y=1.1* 
(max(data$'Baseline', data$'Endline')), hjust=-0.1, size=5)

p + theme(panel.background= element_blank(),
      panel.grid = element_blank(),
      axis.ticks = element_blank(),
      axis.text.x = element_blank(),
      panel.border = element_blank(),
      plot.margin = unit(c(1,2,1,2), "cm"))

0 Answers0