Основываясь на ответах Эндрю Фокса и Cebe, я развернул их и сделал их строковыми расширениями вместо расширений Base64String.
public static class StringExtensions
{
public static string ToBase64(this string text)
{
return ToBase64(text, Encoding.UTF8);
}
public static string ToBase64(this string text, Encoding encoding)
{
if (string.IsNullOrEmpty(text))
{
return text;
}
byte[] textAsBytes = encoding.GetBytes(text);
return Convert.ToBase64String(textAsBytes);
}
public static bool TryParseBase64(this string text, out string decodedText)
{
return TryParseBase64(text, Encoding.UTF8, out decodedText);
}
public static bool TryParseBase64(this string text, Encoding encoding, out string decodedText)
{
if (string.IsNullOrEmpty(text))
{
decodedText = text;
return false;
}
try
{
byte[] textAsBytes = Convert.FromBase64String(text);
decodedText = encoding.GetString(textAsBytes);
return true;
}
catch (Exception)
{
decodedText = null;
return false;
}
}
}
Этот вопрос, вероятно, был задан и до ответа. Тем не менее, есть еще одна проблема при настройке данных.
OP создает данные с помощью
d <- data.frame(x = 1:10,
y = 1:10,
col = c(rep("red", n = 5), rep("green", n = 5)))
. Это приводит к чередованию двух цветов
d
# x y col
#1 1 1 red
#2 2 2 green
#3 3 3 red
#4 4 4 green
#5 5 5 red
#6 6 6 green
#7 7 7 red
#8 8 8 green
#9 9 9 red
#10 10 10 green
Причина в том, что n
не является определенным параметром для функции rep()
. Согласно ?rep
, действительными параметрами являются times
, lenght.out
и each
.
Вероятно, OP означал
d <- data.frame(x = 1:10,
y = 1:10,
col = c(rep("red", 5), rep("green", 5)))
, что приводит к последовательным строкам окрашиваются в один цвет:
d
# x y col
#1 1 1 red
#2 2 2 red
#3 3 3 red
#4 4 4 red
#5 5 5 red
#6 6 6 green
#7 7 7 green
#8 8 8 green
#9 9 9 green
#10 10 10 green
Кстати,
col = c(rep("red", 5), rep("green", 5))
можно более четко записать как
col = rep(c("red", "green"), each = 5)
При этом следующие операторы сюжета
library(ggplot2)
# variant 1 (OP's own answer)
ggplot(data = d, aes(x = x, y = y)) + geom_point(colour = d$col)
# variant 2 (aosmith' comment, more "ggplot2-like")
ggplot(data = d, aes(x = x, y = y, colour = col)) + geom_point() +
scale_colour_identity()
создают одну и ту же диаграмму:
Просто выяснили, как:
цвет следует указывать как отдельный вектор и не связан с данными
ggplot(data=d,aes(x=x,y=y))+geom_point(colour=d$col)