r - Adjust labels of ggplot2 legend -
i've been searching internet last couple of hours not find solution works me. i'm new r
, think miss something.
i try use ggplot2
draw simple plot file in csv
format (;
-separated).
here script wrote:
library(ggplot2) library(reshape) df = read.csv2('test.csv', dec='.') column_names = colnames(df) # melt data each row unique id-variable combination df = melt(df, id=c(column_names[1])) # plot data ggplot(data=df, aes(x=df[, 1], y=value, colour=variable, shape=variable)) + geom_point() + # add points geom_line() + # add initial line plot xlab('x') + # set xlabel ylab('y') + # set ylabel ggtitle('mytitle') + # set title scale_x_log10(breaks=df[[column_names[1]]]) + # df[[]] accesses atomic column)) theme_minimal() + theme(legend.position="right", legend.title=element_blank())
and test input, test.csv
:
column 1;column 2;column 3 111.12;4313.5;6678.25 222.9;9386.0;12372.5
so far, - plot generated. legend contains entries column.2
, column.3
. know due make.names
when file opened using read.csv2
function.
however, did not find out how can modify legend entries such names in csv
file presented there, i.e., column 2
, column 3
.
i tried extract colnames
before make.names
applied , use scale_*
function did not succeed:
library(ggplot2) library(reshape) df = read.csv2('test.csv', check.names=f, dec='.') column_names_str = colnames(df)[-1] colnames(df) = make.names(colnames(df)) column_names = colnames(df) # melt data each row unique id-variable combination df = melt(df, id=c(column_names[1])) # plot data ggplot(data=df, aes(x=df[, 1], y=value, colour=variable, shape=variable)) + geom_point() + # add points geom_line() + # add initial line plot xlab('x') + # set xlabel ylab('y') + # set ylabel ggtitle('mytitle') + # set title scale_x_log10(breaks=df[[column_names[1]]]) + # df[[]] accesses atomic column)) theme_minimal() + theme(legend.position="right", legend.title=element_blank()) + scale_colour_hue(labels=column_names_str)
the above script changes labels splits legend 2 parts (shape , colour). keep combined legend (shape , colour) proper labels.
here did:
ggplot(data=df, aes(x=df[, 1], y=value, colour=variable, shape=variable)) + geom_point() + # add points geom_line(show.legend = true) + # add initial line plot xlab('x') + # set xlabel ylab('y') + # set ylabel ggtitle('mytitle') + # set title scale_x_log10(breaks=df[[column_names[1]]]) # df[[]] accesses atomic column)) #theme_minimal() + #theme(legend.position="right", legend.title=element_blank()) + #scale_colour_hue(labels=column_names_str)
please check if looking for.
Comments
Post a Comment