Facet Grid with Time Series - time-series

I am trying to create a time series graph by year using facet_grid so that the data is plotted above each other. However, when I plot it, there is a lot of blank space due to how the data is broken out (ie the 2017 graph still shows the time series for 2018 and 2019 but with no data, I would like to remove this blank space). How can I free the scale of the x coordinates so it does not show all years?
My Code
proportion_data %>%
ggplot(aes(x = new_date, group = 1)) +
geom_line(aes(y = prop_elect, color ="elect_prop")) +
geom_line(aes(y = prop_water, color ="water_prop")) +
geom_line(aes(y = prop_natgas, color ="natgas_prop")) +
facet_grid(Year ~., scales = "free" )
Utilities by Year
My Data
enter image description here
Any advice would be much appreciated!
Thanks,
Kris

Have you tried using the space option?
facet_grid(Year ~., scales = "free" ,space="free_x")
Another option is to use facet_wrap instead of facet_grid
facet_wrap(~Year, scales = "free" )

Related

How to implement an "Excel like" "add cell" into a canvas

I'm sorry for the title, that maybe can't describe properly what I would like to achieve. I'm starting to develop a new software which should present a "grid" to the user that can be manipulated by him adding "rows" or "columns" in any point of this "grid". The problem is that I'm not sure a real grid is the suitable solution, because there are some "graphical" requirements like changing invididual cells sizes, nesting them, zooming/stretching, etc. So I was starting to analyze a solution in WPF that uses DrawingVisual elements (for performance reason).
I'm able to draw the "grid" in the desired way. I'm also able to add rows or columns at the edges of the drawing. But I can't figure any solution to modify it in the "middle" (except redrawing the whole thing). I'll explain me better with an image. On the left there's the "grid" after it has been drawn for the first time. On the right there's a new grid that should be drawn after the user performs an operation.
An more complex example is the following, where the "row" is added inside an existing cell, causing all the cells to "grow".
As I said, I know I could redraw the whole thing, but I'm concerned about performance. Keep in mind that in a real scenario there could be thousands of blocks and many nesting levels.
Any suggestion is appreciated. The use of WPF is not mandatory, but it will be a desktop app in .NET 5.0. The use of a DrawingVisual is neither mandatory. I can evaluate any solution. Thank you.
A simple technique is to keep positions of columns relative to the left of the canvas in a variable when you first draw the tables. When you want to add a new column, you can crop the image from that point, and in a larger canvas, copy the left and right pieces and just draw the middle column from the beginning.
Of course, the coordinates of each column can be calculated with image processing techniques, but it reduces performance.
I wrote this code with Python, but I do not think it would be difficult to convert it to C#.
import cv2
import numpy as np
# copy image over another
def imdraw(im, over, x, y):
y1, y2 = y, y + over.shape[0]
x1, x2 = x, x + over.shape[1]
for c in range(0, 3):
im[y1:y2, x1:x2, c] = over[:, :, c]
return im
pt = 220
col = 300
off = 15
im = cv2.imread("grid.png", 1)
h, w = im.shape[:2]
crop_left = im[0 : 0 + h, 0:pt]
crop_right = im[0 : 0 + h, pt:w]
cv2.imwrite("left.jpg", crop_left)
cv2.imwrite("right.jpg", crop_right)
# Create an Empty image with white background
out = 255 * np.ones(shape=[h, w + col, 3], dtype=np.uint8)
out = imdraw(out, crop_left, 0, 0)
out = imdraw(out, crop_right, pt + col, 0)
out = cv2.rectangle(
out,
pt1=(pt + off, off),
pt2=(pt + col - off, h - off),
color=(128, 0, 200),
thickness=5,
lineType=cv2.LINE_AA,
)
cv2.imwrite("out.jpg", out)
Output:

How to shift data on xAxis to right on Daniel Gindi charts?

I have a data chart DataChart would like to shift data of xAxis along with data lines to right by certain value.
DataChart Screen Shot
Please help me out which property to use from the library that would be able to solve this purpose.
I am using Daniel Gindi line charts for this purpose.
I use this to create an offset to the left of my chart
myChart.xAxis.spaceMin = 10
maybe it'll do for you
And to align the yAxis lines in front of the labels, i think you could remove the background grid and create your own limit lines if you don't find any other way around
is it viable to subtract the distance d from the x values? that would shift it rightward.
d = 7.0
rightShifted = newGraphData.values.map{ChartDataEntry(x:$0.x-d, y:$0.y)}

How to add description on each category in Core Plot

I've developed a graph for Mac OSX in Xcode5 using Core Plot it looks like this
but in the core plot examples it shows a graph with description of the value on each category like this:
I want to do something similar with each period, I want to add on the top the value like this:
abr 14 = 1500, may 14 = 0
for being more specific, how to do that???
You want to add data labels to one of the plots (probably the one with the red line). Set the labelFormatter and labelTextStyle to turn the labels on and labelOffset and labelRotation to adjust the position and rotation.

Repeating 2d world

How to make a 2d world with fixed size, which would repeat itself when reached any side of the map?
When you reach a side of a map you see the opposite side of the map which merged togeather with this one. The idea is that if you didn't have a minimap you would not even notice the transition of map repeating itself.
I have a few ideas how to make it:
1) Keeping total of 3x3 world like these all the time which are exactly the same and updated the same way, just the players exists in only one of them.
2) Another way would be to seperate the map into smaller peaces and add them to required place when asked.
Either way it can be complicated to complete it. I remember that more thatn 10 years ago i played some game like that with soldiers following each other in a repeating wold shooting other AI soldiers.
Mostly waned to hear your thoughts about the idea and how it could be achieved. I'm coding in XNA(C#).
Another alternative is to generate noise using libnoise libraries. The beauty of this is that you can generate noise over a theoretical infinite amount of space.
Take a look at the following:
http://libnoise.sourceforge.net/tutorials/tutorial3.html#tile
There is also an XNA port of the above at: http://bigblackblock.com/tools/libnoisexna
If you end up using the XNA port, you can do something like this:
Perlin perlin = new Perlin();
perlin.Frequency = 0.5f; //height
perlin.Lacunarity = 2f; //frequency increase between octaves
perlin.OctaveCount = 5; //Number of passes
perlin.Persistence = 0.45f; //
perlin.Quality = QualityMode.High;
perlin.Seed = 8;
//Create our 2d map
Noise2D _map = new Noise2D(CHUNKSIZE_WIDTH, CHUNKSIZE_HEIGHT, perlin);
//Get a section
_map.GeneratePlanar(left, right, top, down);
GeneratePlanar is the function to call to get the sections in each direction that will connect seamlessly with the rest of your world.
If the game is tile based I think what you should do is:
Keep only one array for the game area.
Determine the visible area using modulo arithmetics over the size of the game area mod w and h where these are the width and height of the table.
E.g. if the table is 80x100 (0,0) top left coordinates with a width of 80 and height of 100 and the rect of the viewport is at (70,90) with a width of 40 and height of 20 you index with [70-79][0-29] for the x coordinate and [90-99][0-9] for the y. This can be achieved by calculating the index with the following formula:
idx = (n+i)%80 (or%100) where n is the top coordinate(x or y) for the rect and i is in the range for the width/height of the viewport.
This assumes that one step of movement moves the camera with non fractional coordinates.
So this is your second alternative in a little bit more detailed way. If you only want to repeat the terrain, you should separate the contents of the tile. In this case the contents will most likely be generated on the fly since you don't store them.
Hope this helped.

More efficient way to plot x-axis points?

I have been working on a project that requires a bar graph to be populated with price results. The chart displays the number of items within a given price range. For instance, if on amazon there are 9 items within the price range of $0-$10 the x-axis would display $0-$10 and the y-axis would be populated with a value of 9.
My bar graph has 8 bars, all with similar price ranges. $0-$10, $10-$20, $20-$30... etc.
My question is this: What is the best way to define those individual points? There is no common price range between these items, so the x-axis cannot be static numbers. They must be dynamically calculated within the range of results.
As such, currently I am creating the x-axis points as follows:
I take the lowest result:
#numbers[0] = results[0];
And I take the highest result:
#numbers[8] = results[-1];
Then I find the median of the two:
#numbers[4] = (#numbers[0]+#numbers[8])/2;
I then repeat the process 6 more times
#numbers[2] = (#numbers[0]+#numbers[4])/2; #numbers[6] = (#numbers[4]+#numbers[8])/2; #numbers[1] = (#numbers[0]+#numbers[2])/2; #numbers[3] = (#numbers[2]+#numbers[4])/2; #numbers[5] = (#numbers[4]+#numbers[6])/2; #numbers[7] = (#numbers[6]+#numbers[8])/2;
This gives me the results I need, but it seems awfully repetitive and I would imagine there is a better way.
I tried creating a loop, but I could not write it in a less verbose manner.
Is there a quicker way to do this, or perhaps something more along the lines of DRY?
Are your bins always of equal size? In your example, all share range=10. If so, then you could do:
binspacing = overall range / (numberofbins-1);
and the position of bin n would be the x-axis position of numbers[0] plus n times the binspacing.

Resources