Adding legend (ggplot) doesn't work - r

I feel like I am asking a totally silly question, but I can't force ggplot to show the legend for lines colours.
The thing is that I have two data frames with the same data, just the first data.frame represents new data (plus additional numbers) and the second represents the old data. I am trying to compare new and old data, thus to understand which is which I have to see the legend. I have tried to use scale_colour_manual, but it still doesn't appear.
I have read a number of various answers on similar questions and non of them worked or led to a better. You can see a simple example of my problem below:
rm(list = ls())
library(ggplot2)
xnew<-3:10
y<-5:12
xold<-4:11
years<-2000:2007
xfact<-rep("x", times=8)
yfact<-rep("y", times=8)
Newdata<-data.frame(indicator=c(xfact,yfact),Years=c(years,years), data=c(xnew,y))
Olddata<-data.frame(indicator=xfact,Years=c(years), data=xold)
graph<-ggplot(mapping=aes(Years, data, group=1)) +
geom_line(,Newdata[Newdata=="x",], size=1.5, colour="lightblue")+
geom_line(,Olddata[Olddata=="x",], size=1.5, colour="orange")+
ggtitle("OLD vs NEW")+
scale_colour_manual(name="Legend", values=c("New"="lightblue", "Old"="orange"))
the result is without the legend.
Thanks for all the help I have already found on this website and thank you in advance for helping to solve this problem.

Legends are created in ggplot by mapping aesthetics to a single variable. Your mistake is that you're trying to set colors manually in each layer.
Newdata$type <- "New"
Olddata$type <- "Old"
all_data <- rbind(Newdata,Olddata)
ggplot(data = all_data[all_data$indicator == 'x',],aes(x = Years,y = data,colour = type)) +
geom_line() +
ggtitle("OLD vs NEW") +
scale_colour_manual(name="Legend", values=c("New"="lightblue", "Old"="orange"))
There are countless examples illustrating this basic technique in ggplot here.

Related

ggplot - gaps in stacked bar plot [duplicate]

I'm using ggplot and I get those weird horizontal lines out of geom_bar. I cannot provide a minimal working example: the same code works with few observations and it relies on data I am importing and transforming. However, I can show the relevant line of codes and cross my fingers someone ran into this issue:
ggplot(data) + geom_bar(aes(x=Horizon, y=Importance, fill=Groups),
position='fill', stat='identity') +
theme_timeseries2() +
scale_fill_manual(values=c('#1B9E77', 'orange2', 'black',
'red2', 'blue4')) +
xlab('') + ylab('')
My personal function, theme_timeseries2() isn't the source of the problem: it happens even if I stop after geom_bar. I checked for missing values in Importance and every other column of my data frame and there are none.
It's also very odd: the white lines aren't the same on the zoomed page as in the plot window of RStudio. They do print in .png format when I save the file, so there really is something going on with those horizontal bars. Any theory about why geom_bar() does this would be highly appreciated.
You can fix it by adding the fill as color. Like this:
geom_bar(aes(x=Horizon, y=Importance, fill=Groups, color=Groups),
position='fill', stat='identity')
This was suggested here.
I'm guessing the lines are due to a plotting bug between observations that go into each bar. (That could be related to the OS, the graphics device, and/or how ggplot2 interacts with them...)
I expect it'd go away if you summarized before ggplot2, e.g.:
library(dplyr);
data %>%
count(Horizon, Groups, wt = Importance, name = "Importance") %>%
ggplot() +
geom_col(aes(x = Horizon, y= Importance, fill = Groups), position = "fill") + ....
Mine went away when changing the size of the graphs in rmarkdown.

Creating a multi-panel plot of a data set grouped by two grouping variables in R

I'm trying to solve the following exercise:
Make a scatter plot of the relationship between the variables 'K1' and 'K2' with "faceting" based on the parameters 'diam' and 'na' (subdivide the canvas by these two variables). Finally, assign different colors to the points depending on the 'thickness' of the ring (don't forget to factor it before). The graph should be similar to this one ("grosor" stands by "thickness"):
Now, the last code I tried with is the following one (the dataset is called "qerat"):
ggplot(qerat, aes(K1,K2, fill=factor(grosor))) + geom_point() + facet_wrap(vars(diam,na))
¿Could somebody give me a hand pointing out where the mistake is? ¡Many thanks in advance!
Maybe you are looking for a facet_grid() approach. Here the code using a data similar to yours:
library(ggplot2)
#Data
data("diamonds")
#Plot
ggplot(diamonds,aes(x=carat,y=price,color=factor(cut)))+
geom_point()+
facet_grid(color~clarity)
Output:
In the case of your code, as no data is present, I would suggest next changes:
#Code
ggplot(qerat, aes(K1,K2, color=factor(grosor)))+
geom_point() +
facet_grid(diam~na)

geom_smooth does not plot line of best fit

I hope this question isn't a duplicate. I tried to find answers per the site's requirements before posting, but since I am so new, the help forums are too foreign to me.
Following Wickham's R for data visualization, I easily used geom_point for an integrated data set, mpg:
simple reference code:
ggplot(data = mpg)+
geom_smooth(mapping = aes(x=displ, y=hwy))+
geom_point(mapping = aes(x=displ, y=hwy))
Excited by this cool plot, I tried to do the same for some personal research data, which describes inteferon-beta production over five time points (A,b,c,d,e instead of numerical data).
I used the same code, essentially:
ggplot(data = ifnonly)+
geom_smooth(mapping = aes(x=HOURS, y=IFNB))+
geom_point(mapping = aes(x=HOURS, y=IFNB))
Unfortunately, the line does not display. In fact, nothing displays until I add the geom_point function. What am I missing here? Is there more complex code required or is there some subtlety that I can apply to future uses of this function and ggplot?
I think you should get your desired output with following one line code
library(ggplot2)
ggplot(mtcars, aes(disp,mpg))+geom_smooth() # one line code where I have mentioned data is mtcars , and disp as x axis and mpg as y axis you could get following output
# please check this link for output
o/p without geom_point
library(ggplot2)
ggplot(mtcars, aes(disp,mpg))+geom_smooth()+geom_point()
o/p with geom_point

Multiple line plot using ggplot2

I am trying to emulate a ggplot of multiple lines which works as follows:
set.seed(45)
df <- data.frame(x=c(1,2,3,4,5,1,2,3,4,5,3,4,5), val=sample(1:100, 13),
variable=rep(paste0("category", 1:3), times=c(5,5,3)))
ggplot(data = df, aes(x=x, y=val)) + geom_line(aes(colour=variable))
I can get this simple example to work, however on a much larger data set I am following the same steps but it is not working.
ncurrencies = 6
dates = c(BTC$Date, BCH$Date, LTC$Date, ETH$Date, XRP$Date, XVG$Date)
opens = c(BTC$Open, BCH$Open, LTC$Open, ETH$Open, XRP$Open, XVG$Open)
categories = rep(paste0("categories", 1:ncurrencies),
times=c(nrow(BTC), nrow(BCH), nrow(LTC), nrow(ETH), nrowXRP), nrow(XVG)))
df = data.frame(dates, opens, categories)
# Plot - Not correct.
ggplot(data=df, aes(x=dates, y=opens)) +
geom_line(aes(colour=categories))
As you can see, the different points are discretised and the y-axis is strange. I am guessing this is a rookie error but I have been going round in circles for a while. Can anyone see it?
P.S. I don't think I can upload the data here as it would be too much code. However, the dataframe is in the same format as the practice example and the categories match up correctly to the x and y data. Therefore I believe it is the way I am defining ggplot - I am relatively new to R.
Thank you Markus and Jan, yes you are correct. df$opens was a factor and changing it to a numeric solved the problem.
opens = as.numeric(c(BTC$Open, BCH$Open, LTC$Open, ETH$Open, XRP$Open, XVG$Open))

R pie charts : How to allocate the colors according to a variable?

I'm making a set of pie charts in R (with a loop) out of subsets from a single data frame. I get nice pie charts but the colors are allocated always in the same order in each pie while I would like to have the colors allocated to the same values, throughout all the pies.
my code is :
for (i in 1:length(voisins)) {
y <- subset(zz, Destination==voisins[i])
pie(y$pc,labels=y$Names, col=terrain.colors(nrow(y)) , main=c(y$country[1]))
I would like to have the same color for each y$Names.
I'm not allowed to put an image here, so I put it there : http://cl.ly/image/080t2Q1v1q3V
It would be easier to read if the same region had the same color ! Is there a way to achieve this ?
Solution :
thank you, Arun, for your answer but unfortunately this doesn't produce the result I want, probably I didn't explain it clearly enough. I believe ggplot is a better graphical package and I will learn how to use it soon, but for the moment I don't have time and your answer anyway put me on the right track. I found a solution : actually if you make a bigger "t" (with more rows) in your example and then take subsets of it resulting in smaller t1, t2, t3, ..., with not always the same t$id, so let's say in t1 we would have id = (2,4,9,3) and in t2 id = (4,6,3), and so on. You will see that the colors are allocated always in the same order, which gives in t1 2=red, 4=green, 9=blue, 3=violet, while in t2 4=red, 6=green and 3=blue. So the colors vary from one pie to another and I want to have always 2=red, 4=green, 3=blue, 9=violet. The solution I found is to merge a color column to t according to t$id, then keep it in the subsets t1, t2, t3, ... and set col=t1$color , col=t2$color, and so on in the pie command. It was finally easy but I had hoped for an automatic solution and I will see Paul's one later. Didier
Let me first state that piecharts are a bad idea to begin with as they have major perceptual problems, see e.g. here.
Then to get back ot the problem, I would use ggplot, and more specifically facetting. First create one piechart:
pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) +
geom_bar(width = 1)
pie + coord_polar(theta = "y")
To make a piechart per level of a variable you can use facet_wrap, e.g. per unique level of the variable gear in mtcars:
pie + coord_polar(theta = "y") + facet_wrap(~ factor(gear))

Resources