merging data that is separated by a space in ggplot2 - r

I am currently plotting my data which displays the activity levels for an animal before and after she gave birth.
On the x axis is date (so, day month here) and on the right is activity. As you can see, there is a small gap which was due to collar malfunction. For visual purposes, is it possible to set the data from July 2015 (and upwards) directly next to April 2015 so that the points are sitting right next to one another rather than being separate? I would provide sample data, however, I am still trying to figure out how to do that, however, my R script is the following:
c<-ggplot(ii,aes(y=ii$newavx2, x=ii$datenew, color=infant))+ ylim(1.5,5.9)+ stat_smooth(method=lm)+ geom_point()
Thank you so much for your help.

The first thought of mine is you should put the color in aes() to geom_point if you want the fitted equation to be linear instead of truncated.
If it's the problem of the missing date, you should consider using factor(x) like
ggplot(ii,aes(y=ii$newavx2, x=factor(ii$datenew)) + geom_point
so that the ggplot2 could merge the near truncated date together without leaving any time gap.
However, I don't know if I understand exactly the question. It'd better if you provide some reproducible data set.

Related

How to plot only within a range defined by x number of bars?

I understand that I can contingently plot on the chart using range of date and time values.
However, I would like to be able to plot using a specific number of candlesticks.
For example, I would like to be able to say something like
// Within the most recent 10 bars
// If close[0] > close[1]
// plotshape()
I have tried implementing numerous variations using barstate.isrealtime or barstate.islast but I keep running into limitations.
One major problem is that, although bar_index[#] works by indexing backwards from the most recent bar, the value of bar_index[10] is not 10 but some number in the thousands (depending on the timeframe of the chart — for me its Daily = 2,616 candles, 1hr = 6,217 candles, 15m = 5,222, etc.). In other words, it counts the number of bars from the oldest bar available.
Since referencing of the bars (starting from most recent) and the index values (starting from the oldest) are conflicting--due to counting from opposite ends--I am not sure how to specify: plotshape() for the most recent 10 bars.
I am also running into trouble due to the fact that bar_index[0] occurs every single iteration of the chart's timeframe--so I am getting caught in recursive calculations when trying to do bar_index[0]-bar_index[10].
It seems that what I need is something like bar_index.islast[10]
The reason that I would like to call a plotshape() based on the number of specified candles versus since x date/time (or within date range (x,y)), is because I want my indicator to function properly regardless of which timeframe my chart is displaying:
If I am showing a monthly chart, I want to plot across the last 10 monthly bars; If I am showing a daily chart, I want to plot across the last 10 daily bars; etc.
If I am forced to use a date range, then this functionality breaks down, since I will be shown increasingly more bars for smaller timeframes.
Caveat**
I am able to kinda make this work by specifying the number of bars from the oldest candlestick by stating something like:
bar_index > 2600 ? color=color.black : na
However, given the fact that every single time frame displays a different number of bars, this is not a workable solution for me.
Thanks for any advice.
UPDATE
I have been hunting around and and found that the functionality I desire is already built into the show_last argument of the various plot()functoins.
Ill leave my question posted, in case it helps someone else.
UPDATE I have been hunting around and and found that the functionality I desire is already built into the show_last = int argument of the various plot() functions.
I'll leave my question posted, in case it helps someone else.

Split x axis for every value in graph, in Power BI

I have the following graph in Power BI:
and I'm trying to recreate the below graph from Excel (note the X axis with the months February and January, with the Operation Text:
As you can see, my attempt only displays the Operation Text rather than the month and Operation Text).
Here is what my current axis looks like:
Changing the axis to have Actual Start Date on top, rather than the Operation Text:
Makes my graph look like this:
Is it possible to achieve the same outcome as in Excel, with the values in my graph showing the two months side by side, for each operation text? If so, how can I do this?
I've tried looking into the X axis 'format' settings and wasn't able to see anything obvious to help achieve this. In addition, unfortunately the graph I'm looking to recreate is only a screenshot so I am unable to see how it achieves that outcome.
EDIT:
As suggested, I attempted to place Operation Text into the Legend field in my Visualisations section but I am unable to, due to the fact that I have two Value fields (the two columns Actual Hours and Estimated Hours).
#CR7SMS is correct. You just need to expand down to the next level using that split arrow in the upper right of the visual so that this:
Becomes this:
Note that to get the axis to look like this, you'll need to sort on month rather than one of the measures.
You'll also want to turn off 'Concatenate labels' toggle in the 'Format > X axis' section of the visual settings.
You would have to drill down one level, using the arrows seen at the top right of your first screenshot. For your specific purposes, the split arrow should do the trick. Hope this helps.
Have you tried moving your date or operation text column in legend section ?..not 100% but something close to your requirement you can achieve

How to combine two data sets into one and plotting one graph with both data sets in R Studio

Currently I have combined the Apple stock market and the Samsung stock market from 2014- 2018. I have combined the Date,Open,High, low and Close using cbind and changed the names so it says Apple/ Samsung.
My problem is with the graph, Now my dataset is combined in columns so I feel like this might be part of the problem, but none the less I would prefer to keep it like that. I would love a graph that would have both of the open figures on it over the years.
If I just use plot(Total$OpenApple, Total$OpenSam) the plot is a huge block compared to the line graph I would like.
Thanks.
Without any example data its difficult to understand your problem fully. However, I would try using the ggplot2 package and dplyr package. Then you can change your data so that OpenApple and OpenSam are both part of the same column, and then use a function from ggplot2 to change the colors of your lines based on what group they are a part of.

heatmap.2 color legend custom bins

Hi there stackoverflow community!
I am a graduate student inquiring for some consultation on an aethetics R problem I am encountering.
The data I am working with is in the form of a VERY large matrix (49x51).
My problem is that my data ranges from very small to very large, with the bulk of my data falling within the "very large" end of the spectrum, so unless I convert my data to log10, the heatmap is rather boring and almost entirely the same color.
The spectrum of my data is totally within the range I am expecting, but I am hoping to display it in a more aesthetic way.
Proposed solution: I think I need to bin my data in a non-uniform way. If you look at the attached image, you will see that their heatmap looks nice and the color key shows the heat spectrum in a non-fixed bin format. I would like to do something like that, however, I am not sure how to declare cutoffs for each bin. I would ideally like to declare the cutoffs.
For example, bin 1 (0-1), bin 2 (2-50), bin 3 (51-5000). As you can see, my bins would not be fixed in equal increments.
I have been using heatmap.2 for this. Thanks so much in advance!
heatmap with color legend in non-uniform bins:
Hey #Punintended and #S Rivero,
I think I have reached the point that my heatmap will only improve marginally. Both of you contributed deeply to this success, so thanks! First, to condense the matrix values as much as possible, I normalized by column. I was then able to assign gradients. This turned out much better than I had hoped. As you can see, most of my data is clustered (check out the density in the key) at very low values, this is okay though, for I am interested in the higher values. I had to use custom color gradients to account for possible instances of colorblind attendees that might look at my poster. Anyways, if you guys have comments or recommendations, they will be much appreciated :). Again, thanks a bunch!
enter image description here

3D plotting in R with categorical data

So I am fairly new to the whole 3D plotting in R thing, and have spent the last day reading up various articles and posts in here and other websites/books. And I have to confess, I am still lost. So any help/advice people can give me will be hugely appreciated!
Ok, so here is my issue (with example data). I have 3 Locations (A-C) sampled for 12 consecutive years (2001-2012) for some measure, with many zeros. So I have this data in a table/matrix/dataframe:
LocationA<-c(0,0,0.83,0.167,0.167,0.083,0.25,0.212,0,0.083,0,0)
LocationB<-c(0,0,0,0,0,0,0,0,0,0.212,0,0)
LocationC<-c(0.633,0.462,0.167,0.467,0,0.167,0.451,0.333,0.462,0.595,0,0)
TestMat<-cbind(LocationA,LocationB,LocationC)
rownames(TestMat)<-c(2001:2012)
What I would like would be similar to a topographical map, with the x-axis being the Location, z-axis the Year, and y-axis the value of the thing I am measuring. I would be potentially open to using bar-plots, but ideally something like wireframe() would be best. I have been messing around with various functions and packages, but I cant quite figure out why what I am doing is not working.
Thank you very much for your help! I am fully aware that I may just be a bit of an idiot and not getting it, so thank you for bearing with me!
Cheers!
EDIT:
So I changed it to a "long-read". The x-values couldnt be non-numeric, so I changed them to x2 (as shown below).
x<-c(rep("LocationA",12),rep("LocationB",12),rep("LocationC",12))
y<-rep(c(2001:2012),3)
z<-c(LocationA,LocationB,LocationC)
x2<-c(rep(1,12),rep(2,12),rep(3,12))
I then used the wireframe() function:
wireframe(z~x2*y,xlab="Location",ylab="Year",zlab="Value",distance=0.4,zoom=0.6,
scales=list(arrows=FALSE,
x=list(at=c(1:3),label=c("A","B","C")),
y=list(at=c(2001:2012),label=c(2001:2012)),
z=list(at=c(0,0.2,0.4,0.6,0.8,1),label=c(0,0.2,0.4,0.6,0.8,1)),
distance=c(2,2,2)),
drape=TRUE)
Now, the thing I cannot figure out, and have been fiddling with, is the colouring. I know that the drape() sub-function allows me to give the lattice a gradient of colour changes, with the higher values showing a different colour to the lower ones. However, I cannot seem to get it to work. It does give me a colour gradation, but the highest value (in this is 0.83) which should be a turquoise colour (the default colour gradient is pink-turquoise - will attempt to change this later), is in fact the colour corresponding to ~0.3 value. There is no error message or warning message attached, and I have no idea why this is happening... Any ideas?
Thanks again! :)

Resources