0

I have 15 measurement points and i defined "renkler" color palette for them. I want to change the color of 2 (red: DEF-2 and DEF-13 points in the ps_no column) in these 15.

My codes are

library(ggplot2)
library(reshape)
dat <- read.delim("a.txt")
dat$Date <- as.Date(dat$Date,"%d/%m/%Y")

# order
dat$parameter <- factor(dat$parameter, levels = c("DEF-2", "DEF-13"))
dat$ps_no <- factor(dat$ps_no, levels = c("DEF-2", "PS.584", "PS.585", "PS.586", "PS.603", "PS.630", "DEF-13", "PS.600", "PS.667", "PS.690", "PS.714", "PS.734", "PS.754", "PS.811", "PS.813"))  

# create own color palette
library(RColorBrewer)
renkler = c(brewer.pal(name="Set2", n = 7), brewer.pal(name="Set2", n = 8))

# Setup plot without facets
p <- ggplot(data = dat, aes(x = Date, y = value)) + 
  geom_line(aes(color = ps_no)) +
  geom_point(aes(color = ps_no)) + 
  scale_color_manual(values = renkler) + # oluşturduğumuz paleti yüklemek için
  scale_x_date(date_breaks = "1 months",date_labels = "%Y-%m",
               limits = as.Date.character(c("01/12/2017","31/12/2018"),
                                          format = "%d/%m/%Y")) +
  ylab("[mm/year]") +
  xlab("") +
  facet_grid(parameter ~ .) +
  theme_bw()

p + theme(
  axis.text.x = element_text(angle = 45, hjust = 1),
  panel.grid.major.x = element_blank(),
  panel.grid.minor.x = element_blank(),
)

and the data output with dput(dat):

structure(list(parameter = structure(c(2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("DEF-13",
"DEF-2"), class = "factor"), ps_no = structure(c(3L, 3L, 3L,
3L, 4L, 4L, 4L, 4L, 5L, 5L, 5L, 5L, 7L, 7L, 7L, 8L, 8L, 8L, 2L,
2L, 2L, 6L, 6L, 6L, 9L, 9L, 9L, 10L, 10L, 10L, 11L, 11L, 11L,
12L, 12L, 12L, 13L, 13L, 13L, 14L, 14L, 14L, 15L, 15L, 15L, 1L,
1L, 1L), .Label = c("DEF-13", "DEF-2", "PS.584", "PS.585", "PS.586",
"PS.600", "PS.603", "PS.630", "PS.667", "PS.690", "PS.714", "PS.734",
"PS.754", "PS.811", "PS.813"), class = "factor"), Date = structure(c(17534,
17546, 17870, 17882, 17534, 17546, 17870, 17882, 17534, 17546,
17870, 17882, 17534, 17546, 17882, 17534, 17546, 17882, 17536,
17557, 17879, 17534, 17546, 17882, 17534, 17546, 17882, 17534,
17546, 17882, 17534, 17546, 17882, 17534, 17546, 17882, 17534,
17546, 17882, 17534, 17546, 17882, 17534, 17546, 17882, 17536,
17549, 17886), class = "Date"), value = c(0, 1.23684, -12.15729097,
-11.4102363, 0, 2.45200798, 1.12950398, -2.76779102, 0, 0.924571,
-7.1917482, -6.2764626, 0, -4.0725265, 0.4847485, 0, 0.290382,
-6.098794, 0, 0.813289109, -0.426076522, 0, 1.7502, -5.139665,
0, -29.67012, -14.956098, 0, 12.8852143, 7.4377433, 0, 1.404183,
-12.426633, 0, -24.09551, -7.619493, 0, -4.194441, -16.258703,
0, -0.835691, -10.504454, 0, 1.311699, 6.30102, 0, -1.49366556,
-1.835284539)), row.names = c(NA, -48L), class = "data.frame")

And also I need to change legend tittle (ps_no) and the texts on the right side of plots (DEF-2 and DEF-13).

Thank you.

Edit:

I filter the data which I want to show different color with using filter command. After filter command, I add a command line for geom_line and another command line for geom_point. It is working in the plot. But this is not the answer literally because the colors in the legend do not change.

So this the the new version of codes:

library(ggplot2)
library(reshape)
dat <- read.delim("aroundDEF.txt")
dat$Date <- as.Date(dat$Date,"%d/%m/%Y")

# order
dat$parameter <- factor(dat$parameter, levels = c("DEF-2", "DEF-13"))
dat$ps_no <- factor(dat$ps_no, levels = c("DEF-2", "PS.584", "PS.585", "PS.586", "PS.603", "PS.630", "DEF-13", "PS.600", "PS.667", "PS.690", "PS.714", "PS.734", "PS.754", "PS.811", "PS.813"))  

# create own color palette
library(RColorBrewer)
renkler = c(brewer.pal(name="Set2", n = 7), brewer.pal(name="Set2", n = 8))
  geom_line(aes(color = ps_no)) +
  geom_line(data=highlight_df, aes(color = ps_no), color='#da0018') +
  geom_point(aes(color = ps_no)) + 
  geom_point(data=highlight_df, aes(color = ps_no), color='#da0018') +

# filter dataframe to get data to be highligheted
highlight_df <- dat %>% 
  filter(ps_no=="DEF-2" | ps_no=="DEF-13")

# Setup plot without facets
p <- ggplot(data = dat, aes(x = Date, y = value)) + 

  scale_color_manual(values = renkler) + 
  scale_x_date(date_breaks = "1 months",date_labels = "%Y-%m",
               limits = as.Date.character(c("01/12/2017","31/12/2018"),
                                          format = "%d/%m/%Y")) +
  ylab("[mm/year]") +
  xlab("") +
  facet_grid(parameter ~ .
             , labeller = as_labeller( c("DEF-2" = "DEF-2 and around", "DEF-13" = "DEF-13 and around"))) +
  theme_bw()

p + theme(
  axis.text.x = element_text(angle = 45, hjust = 1),
  panel.grid.major.x = element_blank(),
  panel.grid.minor.x = element_blank(),
)

In short, still I need an answer...

volkan
  • 195
  • 7
  • Does this answer your question? [r - ggplot2 - highlighting selected points and strange behavior](https://stackoverflow.com/questions/11467965/r-ggplot2-highlighting-selected-points-and-strange-behavior) – tjebo May 09 '20 at 14:26
  • Hi @Tjebo, I tried your suggestion but I couldn't achieve the result. – volkan May 09 '20 at 15:02
  • Can you share your dataset via `dput(dat)` (just paste the output from that function here)? Not a good idea to share via a link to a personal google drive here and not many will click for that anyway. – chemdork123 May 09 '20 at 18:10
  • Hi @chemdork123, I pasted the dataset to main question. – volkan May 09 '20 at 18:34

1 Answers1

0

After renkler variable:

renkler[1]= "#DA0018"
renkler[7]= "#DA0018"

For the legend title:

scale_color_manual(values = renkler, name="new name")
volkan
  • 195
  • 7