Question: I have a DataFrame with 2 columns of strings, imported from a tsv file. Both columns need to be converted to ascii. (This is because I want to pass the text through a CountVectorizer and TfidfTransformer pipeline in scikit-learn).

I have gone through dozens of posts both on stackoverflow as well as outside, but cannot figure this one out. My code is below, including some of the things I have tried.

Any suggestions to make this work?

```
# tried including adding encoding="utf-8", does not work
df = pd.read_csv(questions, usecols = [3, 4, 5], nrows = 10, header=0, sep="\t") 

y = df["is_duplicate"].values
X = df.drop("is_duplicate", axis=1).values

for col in X:
    X = X.encode('utf-8') # does not work

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.3,
random_state = 21, stratify = y)

def flat_list(my_list):
    return [str(item) for sublist in my_list for item in sublist]

def transform_data(trans_obj_list,dataset_splits):
    X_train = dataset_splits[0].astype(str)

X_train = flat_list(X_train)

for trfs in trans_obj_list:
    transformed_vector = trfs().fit(X_train)
    for x in range(0,len(dataset_splits)):
        dataset_splits[x] =flat_list(dataset_splits[x].astype(str))

return dataset_splits

new_X_train, new_X_test = transform_data([CountVectorizer,TfidfTransformer],
[X_train, X_test])

```

Answer: 
Solution by passing lines as seperate `data.frame`, setting individual line colour by `color = Group` and specifying those colours with `scale_color_manual`.

```
library(ggplot2)

# Generate data
dBox <- data.frame(y = rnorm(10))
dLines <- data.frame(X =c(-0.362, -0.35),
                     Y = c(0.6, 0.8),
                     Xend = c(0.363, 0.35),
                     Yend=c(0.6, 0.8),
                     Group = c("typeA", "typeB"),
                     color = c("black", "red"))

ggplot(dBox, aes(0, y)) +
    geom_boxplot(outlier.shape = 1)+ 
    scale_y_continuous(name = "",limits = c(0,1))+
    scale_x_discrete(name = "") +
    geom_segment(data = dLines, 
                 aes(x = X, xend = Xend, 
                     y = Y, yend = Yend, 
                     color = Group)) +
    scale_color_manual(values = dLines$color) +
    theme_bw() +
    theme(legend.title = element_blank()) +
    theme(legend.background = element_rect(fill = "white",
                                           size = 0.1, 
                                           linetype = "solid", 
                                           colour = "black"))

```

[![enter image description here](https://i.stack.imgur.com/ft0mR.png)](https://i.stack.imgur.com/ft0mR.png)
I augmented PoGibas very helpful answer to two different linetypes as well:

```
y = data.frame(runif(500))

dLines <- data.frame(X =c(-0.362, -0.35),
                 Y = c(0.6, 0.8),
                 Xend = c(0.363, 0.35),
                 Yend=c(0.6, 0.8),
                 Group = c("TypeA", "TypeB"),
                 color = c("black", "red"),
                 linetype = c( "solid", "dashed"))

ggplot(data = y )+
  aes(0, y)+
  geom_boxplot(outlier.shape = 1)+ 
  scale_y_continuous(name = "",limits = c(0,1))+
  scale_x_discrete(name = "")+
  geom_segment(data = dLines, 
           aes(x = X, xend = Xend, 
               y = Y, yend = Yend, 
               color = Group,
               linetype = Group))+
  scale_color_manual(values = dLines$color) +
  scale_linetype_manual(values = dLines$linetype) +
  theme_bw()+
  theme(legend.title = element_blank())+
  theme(legend.background = element_rect(fill="white",
                                     size=0.1, linetype="solid", 
                                     colour ="black"))

```

[![enter image description here](https://i.stack.imgur.com/kCQ4t.png)](https://i.stack.imgur.com/kCQ4t.png)